接口方法的成员有不同的类型

时间:2010-01-16 22:13:16

标签: c# interface types member

我有这个界面

public interface TestInterface
{
   [returntype] MethodHere();
}

public class test1 : TestInterface
{
   string MethodHere(){
      return "Bla";
   }
}

public class test2 : TestInterface
{
   int MethodHere(){
     return 2;
   }
}

有没有办法让[returntype]动态?

2 个答案:

答案 0 :(得分:5)

将返回类型声明为Object,或使用通用接口:

public interface TestInterface<T> {
    T MethodHere();
}

public class test3 : TestInterface<int> {
   int MethodHere() {
      return 2;
   }
}

答案 1 :(得分:4)

不是动态,但您可以将其设为通用:

public interface TestInterface<T>
{
    T MethodHere();
}

public class Test1 : TestInterface<string>
... // body as before
public class Test2 : TestInterface<int>
... // body as before

如果这不是您所追求的,请详细说明您希望如何使用界面。

相关问题