返回具有通用类型的类的函数,返回具有特定类型的类的函数

时间:2020-09-10 15:14:57

标签: c# generics polymorphism factory

如果我具有以下功能:

randomClassInstance.getName(year)

public IGenericInterface<T> GetSomething<T>(T GenericType) { switch (GenericType) { case SpecificType spec: return new DerivedClass(); default: return null; } } 源自DerivedClass。但是我得到IGenericInterface<SpecificType>

为什么会这样,这是否有可能做到?如何实现这样的函数以返回Cannot implicitly convert type DerivedClass to IGenericInterface<T>

的任何特定实现

0 个答案:

没有答案
相关问题