【发布时间】:2011-09-30 13:21:07
【问题描述】:
我正在尝试创建一个包含通用委托的接口。然后我希望实现接口的类决定实际的类型方法,或者最好甚至返回另一个委托。
下面是一些代码,描述了我想要实现的目标。
public delegate void GenericMethod<T>(T arg);
public delegate void StringMethod(string str);
public delegate void ByteMethod(byte bt);
public interface ITest
{
GenericMethod<T> someMethod;
}
public class TestA : ITest
{
public GenericMethod<string> someMethod
{
get
{
return stringMethod; //which is of type StringMethod(string str), defined above
}
}
}
public class TestB : ITest
{
public GenericMethod<byte> someMethod
{
get
{
return byteMethod; //which is of type ByteMethod(byte bt);, defined above
}
}
}
这可能吗?或者以这种方式切换代表是不可能的?
【问题讨论】:
标签: c# generics interface delegates