繁体   English   中英

System.Int32是否有内置的IEqualityComparer <T>?

[英]Is there a built-in IEqualityComparer<T> for System.Int32?

我们目前使用EqualityComparer<TKey>.Default作为初始化通用字典的默认方式。 但是,当字典的键是int类型时,它会在Xamarin.iOS上崩溃并出现以下错误(但在我尝试过的许多其他平台上都有效):

尝试使用JIT编译方法Lucene.Net.Support.LurchTable2<Lucene.Net.Facet.Taxonomy.FacetLabel, Lucene.Net.Facet.Taxonomy.Directory.DirectoryTaxonomyReader/Int32Class>:InternalInsert<Lucene.Net.Support.LurchTable2/Add2Info<Lucene.Net.Facet.Taxonomy.FacetLabel, Lucene.Net.Facet.Taxonomy.Directory.DirectoryTaxonomyReader/Int32Class>> (int,Lucene.Net.Facet.Taxonomy.FacetLabel,int&,Lucene.Net.Support.LurchTable/Add2Info<Lucene.Net.Facet.Taxonomy.FacetLabel, Lucene.Net.Facet.Taxonomy.Directory.DirectoryTaxonomyReader/Int32Class>)在仅以aot模式运行时。 有关更多信息,请参阅https://developer.xamarin.com/guides/ios/advanced_topics/limitations/

at Lucene.Net.Support.LurchTable2 [TKey,TValue] .Insert [T](TKey key,T&value)<0x2570f48 + 0x000e0> in <063e095c95d945a4ace32ab83d1227eb#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at(wrapper unknown)System.Object.gsharedvt_in() at Lucene.Net.Support.LurchTable2 [TKey,TValue] .AddOrUpdate(TKey key,TValue addValue,Lucene.Net.Support.KeyValueUpdate2 [TKey,TValue] fnUpdate)<0x232824c + 0x0013b> in <063e095c95d945a4ace32ab83d1227eb#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at Lucene.Net.Facet.Taxonomy.LRUHashMap2 [TKey,TValue] .Put(TKey key,TValue value)<0x2c487f8 + 0x0015b> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at Lucene.Net.Facet.Taxonomy.Directory.DirectoryTaxonomyReader.GetOrdinal (Lucene.Net.Facet.Taxonomy.FacetLabel cp)<0x2c51970 + 0x0019b> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at Lucene.Net.Facet.Taxonomy.Int32TaxonomyFacets.GetTopChildren(System.Int32 topN,System.String dim,System。 String [] path)<0x2c481dc + 0x0008f> i Ñ<79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0在Login.MyMB.Lucene.Client.LuceneArticoliSearcher.GetListaArticoloXRicercaAvanzataConRicercaSemplice(System.Collections.Generic.List1 [T] listParametri)<0x224add0 + 0x001bb>在<8f49891e0f0546e185aba7424d294ef7#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0在Login.MyMB .Lucene.Client.LuceneArticoliSearcher.GetListaArticoloConRicercaSemplice(System.Collections.Generic.List1 [T] listParametri)<0x224afbc + 0x0009f>在<8f49891e0f0546e185aba7424d294ef7#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0在MyMB.Forms.RicercaLucene.RicercaArticoloLucene.GetListaArticoliXRicercaSemplice(Login.MyMB.Interface .IAmbiente ambiente,Login.MyMB.Lucene.Client.LuceneArticoliSearcher las,System.Collections.Generic.List`1 [T] ListParametri,System.Boolean isAbilitataRicercaBarcode)<0xe47fc0 + 0x000e7> in:0 ........ .......................

在链接https://docs.microsoft.com/it-it/xamarin/ios/internals/limitations ,我发现了问题原因(我想......):

值类型作为字典键使用值类型作为Dictionary<TKey, TValue>键是有问题的,因为默认的Dictionary构造函数尝试使用EqualityComparer<TKey>.Default EqualityComparer<TKey>.Default反过来尝试使用Reflection来实例化实现IEqualityComparer<TKey>接口的新类型。 这适用于引用类型(因为反射+创建新类型步骤被跳过),但对于值类型,一旦您尝试在设备上使用它,它会很快崩溃和烧毁。 解决方法:在新类型中手动实现IEqualityComparer<TKey>接口,并向Dictionary<TKey, TValue>IEqualityComparer<TKey> )构造函数提供该类型的实例。

是否有内置的跨平台方法来创建默认的IEqualityComparer<int> 我试图避免创建这样的课程

private class Int32EqualityComparer : IEqualityComparer<int>
{
    bool IEqualityComparer<int>.Equals(int x, int y)
    {
        return x.Equals(y);
    }

    int IEqualityComparer<int>.GetHashCode(int obj)
    {
        return obj.GetHashCode();
    }
} 

只是为了绕过这个错误。

您可以通过搜索API参考来确定.NET Standard中没有IEqualityComparer<int>实现。

如果您打算自己实现IEqualityComparer<T> ,那么使用T约束到IEquatable<T>的通用实现可能是值得的,这样您就可以覆盖多种类型而不仅仅是int 建议所有值类型按照框架设计指南实现IEquatable<T>

看起来不像它, IEqualityComparer<T>的.NET Standard 2.0文档中的派生类型列表没有为整数列出任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM