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

5

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

试图在aot-only模式下编译方法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>)。有关更多信息,请参见https://developer.xamarin.com/guides/ios/advanced_topics/limitations/
在运行中,调用了Lucene.Net.Support.LurchTable2[TKey,TValue].Insert[T] (TKey key, T& value) <0x2570f48 + 0x000e0> in <063e095c95d945a4ace32ab83d1227eb#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0。然后调用了Lucene.Net.Support.LurchTable2[TKey,TValue].AddOrUpdate (TKey key, TValue addValue, Lucene.Net.Support.KeyValueUpdate2[TKey,TValue] fnUpdate) <0x232824c + 0x0013b> in <063e095c95d945a4ace32ab83d1227eb#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0、Lucene.Net.Facet.Taxonomy.LRUHashMap2[TKey,TValue].Put (TKey key, TValue value) <0x2c487f8 + 0x0015b> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0和Lucene.Net.Facet.Taxonomy.Directory.DirectoryTaxonomyReader.GetOrdinal (Lucene.Net.Facet.Taxonomy.FacetLabel cp) <0x2c51970 + 0x0019b> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0。
然后调用了Lucene.Net.Facet.Taxonomy.Int32TaxonomyFacets.GetTopChildren (System.Int32 topN, System.String dim, System.String[] path) <0x2c481dc + 0x0008f> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0,以及Login.MyMB.Lucene.Client.LuceneArticoliSearcher.GetListaArticoloXRicercaAvanzataConRicercaSemplice (System.Collections.Generic.List1[T] listParametri) <0x224add0 + 0x001bb> in <8f49891e0f0546e185aba7424d294ef7#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0和Login.MyMB.Lucene.Client.LuceneArticoliSearcher.GetListaArticoloConRicercaSemplice (System.Collections.Generic.List1[T] listParametri) <0x224afbc + 0x0009f> in <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 isAbilit
在链接https://learn.microsoft.com/it-it/xamarin/ios/internals/limitations上,我找到了问题的原因(我猜是这个):

使用值类型作为Dictionary<TKey, TValue>的键是有问题的,因为默认的Dictionary构造函数会尝试使用EqualityComparer<TKey>.DefaultEqualityComparer<TKey>.Default又会尝试使用反射来实例化一个实现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();
    }
} 

只是为了避免这个错误。

2个回答

5
根据搜索API参考,在.NET标准中没有对,那么最好使用泛型实现,将T约束为IEquatable,这样可以覆盖多种类型,而不仅仅是int。根据框架设计指南,建议所有值类型都实现IEquatable。请注意保留HTML标记。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接