【问题标题】:BCL type that implements two instances of the same generic type实现相同泛型类型的两个实例的 BCL 类型
【发布时间】:2014-03-14 21:37:29
【问题描述】:

.NET 框架中的任何类或 MSDN 库中记录的任何其他库是否显式地实现了同一通用接口的两个实例化?

例如,以下类显式实现了IEnumerable<int>IEnumerable<string>

public class T : IEnumerable<int>, IEnumerable<string>
{
    IEnumerator<string> IEnumerable<string>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<int> IEnumerable<int>.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

对于任何想知道的人,我正在努力解决 Sandcastle 帮助文件生成器无法解析 MSDN 文档 URL 以继承显式实现的接口方法的问题。例如,扩展Dictionary&lt;TKey, TValue&gt; 的类继承ICollection&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;.Contains(KeyValuePair&lt;TKey, TValue&gt;) 的显式实现。使用MTPS查找该方法时,contentIdentifier如下:

AssetId:M:System.Collections.Generic.Dictionary`2.System#Collections#Generic#ICollection{T}#Contains(System.Collections.Generic.KeyValuePair{`0,`1 })

如果您注意到其中的粗体 T,您会发现此语法无法区分我在上面询问的具体情况。通过定位框架中的现有类,我可以使用 MTPS 服务来检查在这种特定类型的冲突情况下使用的内容标识符,从而确保我在这种肯定很少见但理论上可能的边缘情况下的行为正确。

【问题讨论】:

标签: .net generics msdn base-class-library


【解决方案1】:

【讨论】:

  • 在每种情况下,显式接口实现实际上是不同类型的,而不是相同的类型。
  • EF 有三种类型可以直接实现多个接口参数化。 System.Data.Entity.ModelConfiguration.Conventions.SqlCePropertyMaxLengthConvention, System.Data.Entity.ModelConfiguration.Conventions.PropertyMaxLengthConvention, System.Data.Entity.ModelConfiguration.Conventions.SqlCePropertyMaxLengthConvention
  • 那些的情况下,不同重载的参数类型区分方法。然而,基于此,我意识到我试图阻止的问题只会影响链接到 MSDN 文档 的代码,所以如果没有 MSDN 文档具有这些特征,那么它真的不可能是一个错误!
猜你喜欢
  • 2020-06-11
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2019-01-20
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-03
相关资源
最近更新 更多