开发者

Members of interface's methods have different types

开发者 https://www.devze.com 2022-12-17 17:50 出处:网络
I have this interface public interface TestInterface { [returntype] MethodHer开发者_StackOverflow中文版e();

I have this interface

public interface TestInterface
{
   [returntype] MethodHer开发者_StackOverflow中文版e();
}

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

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

Is there any way to make [returntype] dynamic?


Either declare the return type as Object, or use a generic interface:

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

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


Not really dynamic but you could make it generic:

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

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

If that's not what you're after, please give more details about how you'd like to be able to use the interface.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号