2010-01-16 67 views
0

我有这个接口成员有不同的类型

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

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

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

有没有什么办法让[返回类型]活力呢?

回答

5

要么声明返回类型为对象,或使用通用接口:

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

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

不是真的动态但你可以使它通用:

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

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

如果不是你以后,请给你想如何能够使用的详细资料接口。

+0

感谢这正是我一直在寻找! – 2010-01-16 22:17:34

相关问题