我有一个简单的程序:
class Program
{
static void Main(string[] args)
{
InA testing = GetA();
}
static InA GetA<InA>()
{
return new A();
}
}
public interface InA
{
void test();
}
public class A : InA
{
public void test()
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不能从GetA()返回A的实例?实现接口InA.
在这种情况下,InA作为通用参数处理,而不是作为接口名称处理.您应该以不同方式声明GetA方法的泛型参数,或者将其删除.这应该工作:
static InA GetA()
{
return new A();
}
Run Code Online (Sandbox Code Playgroud)