我想找到泛型T的类型,并在运行时进行比较以检查它是哪个接口。

因此,这适用于在运行时查找T的类型:

Type interfaceName = typeof(T); // gives me the specific interface type


但是,当我尝试检查它是否等于接口的类型时,我没有得到预期的响应。

Type.GetType("IMyInterface"); //this returns null


如何比较这两个?

最佳答案

如果要使用Type.GetType,则需要传递assembly-qualified name

但是,您似乎只想检查一个接口的名称,因此您可以简单地使用

Type interfaceType = typeof(T);
string interfaceName = interfaceType.Name;


您也可以简单地查看typeof(T) == typeof(IMyInterface)

关于c# - 将通用T的类型解析为C#中的特定接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25746982/

10-11 15:40