gpt4 book ai didi

c# - 是否有用于 System.Int32 的内置 IEqualityComparer

转载 作者:太空狗 更新时间:2023-10-30 00:48:02 26 4
gpt4 key购买 nike

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

Attempting to JIT compile method 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>) while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.

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> in <79d3a7b905954d0993025c09c5d087ce#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at Login.MyMB.Lucene.Client.LuceneArticoliSearcher.GetListaArticoloXRicercaAvanzataConRicercaSemplice (System.Collections.Generic.List1[T] listParametri) <0x224add0 + 0x001bb> in <8f49891e0f0546e185aba7424d294ef7#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at Login.MyMB.Lucene.Client.LuceneArticoliSearcher.GetListaArticoloConRicercaSemplice (System.Collections.Generic.List1[T] listParametri) <0x224afbc + 0x0009f> in <8f49891e0f0546e185aba7424d294ef7#2ae0fea9ea4eacaef83bf2e9713bb8ea>:0 at 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://learn.microsoft.com/it-it/xamarin/ios/internals/limitations ,我找到了问题的原因(我想......):

Value types as Dictionary Keys Using a value type as a Dictionary<TKey, TValue> key is problematic, as the default Dictionary constructor attempts to use EqualityComparer<TKey>.Default. EqualityComparer<TKey>.Default, in turn, attempts to use Reflection to instantiate a new type which implements the IEqualityComparer<TKey> interface. This works for reference types (as the reflection+create a new type step is skipped), but for value types it crashes and burns rather quickly once you attempt to use it on the device. Workaround: Manually implement the IEqualityComparer<TKey> interface in a new type and provide an instance of that type to the Dictionary<TKey, TValue> (IEqualityComparer<TKey>) constructor.

问题

是否有内置的跨平台方式来创建默认 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();
}
}

只是为了解决这个错误。

最佳答案

没有执行 IEqualityComparer<int>在 .NET Standard 中,您可以通过搜索 API reference 来确定.

如果您打算实现 IEqualityComparer<T>你自己,可能值得用 T 做一个通用的实现限于 IEquatable<T>这样你就可以涵盖多种类型而不仅仅是 int .所有值类型都推荐实现 IEquatable<T>根据 Framework design guidelines .

关于c# - 是否有用于 System.Int32 的内置 IEqualityComparer<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com