0

我有这个界面

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

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

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

有什么方法可以使 [returntype] 动态化吗?

4

2 回答 2

5

要么将返回类型声明为 Object,要么使用泛型接口:

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

public class test3 : TestInterface<int> {
   int MethodHere() {
      return 2;
   }
}
于 2010-01-16T22:15:20.143 回答
4

不是很动态,但你可以让它通用:

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

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

如果这不是您所追求的,请提供有关您希望如何使用该界面的更多详细信息。

于 2010-01-16T22:14:39.997 回答