【问题标题】:.NET Reflection - Results of GetInterfaces() of generic type definition cannot be successfully compared to generic typeof literal.NET 反射 - 泛型类型定义的 GetInterfaces() 结果无法成功地与泛型 typeof 文字进行比较
【发布时间】:2019-01-22 15:39:08
【问题描述】:

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

我必须通过从 LINQ Expression 中提取 Type 对象来确定类型是否为 IDictionary<,>,因此我无法使用 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<,>))

我假设因为我正在阅读泛型类型定义的接口,所以我会得到泛型接口定义。更奇怪的是,当我将上面的代码粘贴到 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 方法中的比较对于这些元素中的任何一个都没有成功。但是,如果我像这样获得接口本身的泛型类型定义:

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

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

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

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

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

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

【问题讨论】:

  • typeof(IDictionary&lt;,&gt;).IsAssignableFrom(i.GetType()) 可以代替i == typeof(IDictionary&lt;,&gt;) 工作吗?
  • @J.vanLangen 不,因为您不能指望该方法能够确定是否可以将某些内容分配给“不确定类型”。但即使是这样,也无法解释我的问题。
  • 可能与 stackoverflow.com/questions/1735035/… 有关,但我现在无法深入研究。

标签: c# .net generics reflection


【解决方案1】:

CodeCaster 已经在 cmets 部分给出了答案。基本上,它与开放/封闭结构有关。 typeof(IDictionary&lt;,&gt;)typeof(IDictionary&lt;string,int&gt;) 不同。而且和typeof(IDictionary&lt;TKey,TValue&gt;)不一样。

基本上,typeof(IDictionary&lt;,&gt;) 是未绑定类型,但 IDictionary&lt;TKey, TValue&gt;,您从 typeof(Dictionary&lt;string, string&gt;).GetGenericTypeDefinition() 获得的是开放但构造的类型,因为 TKeyTValue 是类型参数。


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

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<,>));

【讨论】:

  • 感谢您的回复,但我不是在寻找可以解决我的问题的代码,因为我已经有了它并将它也包含在我的问题中(通过这样做,在界面上调用 GetGenericTypeDefinition()我的问题是我调用GetInterfaces() 方法的类型本身已经是一个泛型类型定义,即它是Dictionary&lt;,&gt;不是 Dictionary&lt;string,int&gt; 或类似的东西。我期待如果没有提供类型参数,那么它不会返回类型化接口......
  • ...它确实没有,LINQPad 告诉我该接口已经是一个通用接口类型定义但由于某种原因,相等性检查不同意。我一直在寻找解释为什么会这样。
  • 我自己做了一些研究并找到了答案。请查看更新后的答案?
  • @Balázs,这应该可以让您更好地理解! docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多