- 爱易网页
-
C#教程
- 关于方法中有泛型接口的调用,该如何处理
日期:2014-05-18 浏览次数:20740 次
关于方法中有泛型接口的调用
服务器定义了 方法调用类
public class TestClass
{
public bool GetVarForBiz( ref IVarForFeeItem<IFeeVariable, IFeeItem> varForFeeItemList )
{
return true;
}
}
服务器端定义了三个接口:
接口1:
public interface IVarForFeeItem<T, U>
where T : IFeeVariable
where U : IFeeItem
{
T VarList
{
get;
set;
}
U FeeItem
{
get;
set;
}
}
接口2:
public interface IFeeVariable
{
string Name
{
get;
set;
}
string Des
{
get;
set;
}
string Type
{
get;
set;
}
string Value
{
get;
set;
}
}
接口3:
public interface IFeeItem
{
string Name
{
get;
set;
}
string FeeCode
{
get;
set;
}
string ShortName
{
get;
set;
}
}
客户端调用:
static void Main(string[] args)
{
TestClass tc = new TestClass();
TestVarForFeeItem t = new TestVarForFeeItem();
IVarForFeeItem<IFeeVariable, IFeeItem> i = (IVarForFeeItem<IFeeVariable, IFeeItem>)t;
bool b = tc.GetVarForBiz(ref t);
Console.ReadLine();
}
这种调用方法失败,请问大家应如何调用呢? 还是需要修改服务器方法的参数呢?
在客户端定义继承自服务器接口的类:
public class TestVarForFeeItem : IVarForFeeItem<TestFeeVariable, TestFeeItem>
{
private TestFeeVariable _VarList;
private TestFeeItem _FeeItem;
public TestFeeVariable VarList
{
get
{
return _VarList;
}
set
{
_VarList = value;
}
}
public TestFeeItem FeeItem
{
get
{
return _FeeItem;
}
set
{
_FeeItem = value;
}
}
}
public class TestFeeItem : IFeeItem
{
private string _Name;
private string _FeeCode;
private string _ShortName;
private string _IsEnable;
private string _MustCharge;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;