c# - .NET 反射 - 泛型类型定义的 GetInterfaces() 结果无法与泛型文字类型成功比较

标签 c# .net generics reflection

我最近不得不在 .NET 中进行一些反射(reflection),并且在比较泛型类型定义时偶然发现了一个奇怪的行为。

我必须决定一个类型是否是 IDictionary<,>通过提取 Type来自 LINQ 的对象 Expression所以我无法使用 is运营商来决定它。此外,我不得不忽略类型参数,换句话说,唯一重要的是我是否正在处理 IDictionary<,>。任何键/值类型。

我从以下几点着手:

// Just a placeholder, in my code I have no way of knowing that it
// is a Dictionary of string keys and string values or even that it
// is a dictionary at all.
typeof(Dictionary<string, string>)
    .GetGenericTypeDefinition()
    .GetInterfaces()
    .Any(i => i == typeof(IDictionary<,>))

我假设因为我正在阅读泛型类型定义的接口(interface),所以我会得到泛型接口(interface)定义。更奇怪的是,当我将上面的代码粘贴到 LINQPad 时,它返回了以下内容:

typeof(IDictionary<TKey,TValue>) // it appears here
typeof(ICollection<KeyValuePair<TKey,TValue>>) 
typeof(IEnumerable<KeyValuePair<TKey,TValue>>) 
typeof(IEnumerable) 
typeof(IDictionary) 
typeof(ICollection) 
typeof(IReadOnlyDictionary<TKey,TValue>) 
typeof(IReadOnlyCollection<KeyValuePair<TKey,TValue>>) 
typeof(ISerializable) 
typeof(IDeserializationCallback) 

但由于某种原因,Any 中的比较方法对这些元素中的任何一个都没有成功。但是,如果我像这样获得接口(interface)本身的通用类型定义:

typeof(Dictionary<string, string>)
    .GetGenericTypeDefinition()
    .GetInterfaces()
    .Select(i => i.IsGenericType ? i.GetGenericTypeDefinition() : i)
    .Any(i => i == typeof(IDictionary<,>))

然后返回 true就像它应该的那样。

这是为什么呢? .GetInterfaces() 返回的接口(interface)不是在泛型类型定义上调用泛型接口(interface)定义本身时的方法?如果是这样,当我在 LINQPad 中检查返回的接口(interface)时,它们似乎是泛型类型定义的解释是什么?

我想这与接口(interface)的类型参数可以完全独立于实现者的通用参数这一事实有关,例如:

public class MyGenericType<TKey, TValue> : ISomeInterface<object> ...

但返回的 Type 看起来还是很奇怪对象似乎并没有反射(reflect)这种情况(正如我在这个特定示例中所期望的那样),并且从“外观”来看,它们似乎是通用类型定义,但它们并未被识别为那样。

最佳答案

CodeCaster已经在评论区给出了答案。基本上,它与开放/封闭结构有关。 typeof(IDictionary<,>)typeof(IDictionary<string,int>) 不同.此外,它与 typeof(IDictionary<TKey,TValue>) 不同。 .

基本上,typeof(IDictionary<,>)是一个未绑定(bind)的类型但是 IDictionary<TKey, TValue> ,你从你的typeof(Dictionary<string, string>).GetGenericTypeDefinition()得到自 TKey 以来是一个开放但构造的类型和 TValue是类型参数。


所以这是一个简单的扩展方法,可以做你想做的事。

public static bool HasGenericInterface(this Type @type,Type genericInterface)
{
    var allInterfaces = @type.GetInterfaces();
    return allInterfaces
        .Where(i => i.IsGenericType)
        .Select(i => i.GetGenericTypeDefinition())
        .Any(i => i.IsAssignableFrom(genericInterface));
 }

简单用法如下

var result = typeof(MyClass).GetType().HasGenericInterface(typeof(IDictionary<,>));

关于c# - .NET 反射 - 泛型类型定义的 GetInterfaces() 结果无法与泛型文字类型成功比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311695/

相关文章:

c# - Wpf 绝对与相对包 URI

java - 使用一种方法,以失败早期的方式干净、安全地将原始 map 转换为通用 map

斯卡拉 : generics and variable inheritance issue

c# - 扩展 C# 语言?

c# - 使用 JSON.NET 进行序列化时,使自定义 JsonConverter 尊重 ItemTypeNameHandling

.net - 无法创建迁移。错误 MSB1009 : Project file does not exist

swift - 在 Swift 泛型类型中包含继承约束

c# - 我正忙于编写 C# IBM MQ 客户端应用程序,并不断收到 "Exception: The type initializer for ' IBM.WMQ.Nmqi.UnmanagedNmqiMQ' 抛出异常。”

c# - 在 ASP.Net MVC Controller 中,如何获得一种方法来识别来自 JSON 和 Form-urlEncoded 格式的数据?

c# - 通知中心传递失败 C#