【发布时间】:2012-04-13 19:41:44
【问题描述】:
我有类似的东西:
public interface IExample
{
int GetInteger()
T GetAnything(); //How do I define a function with a generic return type???
^^^^^
}
这可能吗???
【问题讨论】:
我有类似的东西:
public interface IExample
{
int GetInteger()
T GetAnything(); //How do I define a function with a generic return type???
^^^^^
}
这可能吗???
【问题讨论】:
如果整个界面应该是通用的:
public interface IExample<T>
{
int GetInteger();
T GetAnything();
}
如果只有方法需要是通用的:
public interface IExample
{
int GetInteger();
T GetAnything<T>();
}
【讨论】:
GetAnything 返回类型Foo,必须拥有ClassImplementingIExample<Foo>。
public interface IExample<T>
{
int GetInteger()
T GetAnything();
}
多达 :) !
或者,您可以直接返回 System.Object 并将其转换为您想要的任何内容。
【讨论】:
return System.Object and cast it to whatever you want.
如果您不希望整个界面(IExample)是通用的,那么您也可以这样做
public interface IExample
{
int GetInteger();
T GetAnything<T>();
}
【讨论】: