我想找到通用 T
的类型并进行比较,以在运行时检查它是哪个接口(interface)。
因此,这适用于在运行时查找 T 的类型:
Type interfaceName = typeof(T); // gives me the specific interface type
但是当我尝试检查它是否等于接口(interface)的类型时,我没有得到预期的响应。
Type.GetType("IMyInterface"); //this returns null
如何比较这两者?
最佳答案
如果要使用Type.GetType
,则需要传递assembly-qualified name .
但是,看起来您只想检查接口(interface)的名称,因此您可以简单地使用
Type interfaceType = typeof(T);
string interfaceName = interfaceType.Name;
您还可以简单地检查是否typeof(T) == typeof(IMyInterface)
关于c# - 将泛型 T 的类型解析为 C# 中的特定接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25746982/