gpt4 book ai didi

c# - C# 中 OrderDictionary 的通用实现显示不明确的方法警告

转载 作者:行者123 更新时间:2023-11-30 20:30:01 27 4
gpt4 key购买 nike

由于在问这个问题时 C# 没有 OrderedDictionary 的通用实现,所以我从 here 下载了一个。 .明确地说,我在 Unity 游戏引擎中使用它和 MonoDevelop 来编写游戏代码。

实现看起来很好地组合在一起,但是它给了我一个模棱两可的方法调用,警告我似乎无法弄清楚的解决方案。有人可以向我解释这里发生了什么,并提出一个可能的解决方案来消除警告吗?

具体来说,这里是类似的方法调用:

IDictionaryEnumerator IOrderedDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}

IDictionaryEnumerator IDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
{
return List.GetEnumerator();
}

这里是错误:

[Warning] [CS0278] `TurboLabz.Game.IOrderedDictionary<string,TurboLabz.Game.RoomInfo>' contains ambiguous implementation of `enumerable' pattern.
Method `System.Collections.Specialized.IOrderedDictionary.GetEnumerator()' is ambiguous with method `System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,TurboLabz.Game.RoomInfo>>.GetEnumerator()'

提前致谢。

编辑:

这是源代码及其在我的代码库中的用法:

IOrderedDictionary.cs

using System.Collections.Generic;
using System.Collections.Specialized;

namespace TurboLabz.Game
{
public interface IOrderedDictionary<TKey, TValue> : IOrderedDictionary, IDictionary<TKey, TValue>
{
new int Add(TKey key, TValue value);
void Insert(int index, TKey key, TValue value);

new TValue this[int index]
{
get;
set;
}
}
}

OrderedDictionary.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace TurboLabz.Game
{
public class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>
{
private const int DefaultInitialCapacity = 0;

private static readonly string _keyTypeName = typeof(TKey).FullName;
private static readonly string _valueTypeName = typeof(TValue).FullName;
private static readonly bool _valueTypeIsReferenceType = !typeof(ValueType).IsAssignableFrom(typeof(TValue));

private Dictionary<TKey, TValue> _dictionary;
private List<KeyValuePair<TKey, TValue>> _list;
private IEqualityComparer<TKey> _comparer;
private object _syncRoot;
private int _initialCapacity;

public OrderedDictionary()
: this(DefaultInitialCapacity, null)
{
}

public OrderedDictionary(int capacity)
: this(capacity, null)
{
}

public OrderedDictionary(IEqualityComparer<TKey> comparer)
: this(DefaultInitialCapacity, comparer)
{
}

public OrderedDictionary(int capacity, IEqualityComparer<TKey> comparer)
{
if(0 > capacity)
throw new ArgumentOutOfRangeException("capacity", "'capacity' must be non-negative");

_initialCapacity = capacity;
_comparer = comparer;
}

private static TKey ConvertToKeyType(object keyObject)
{
if(null == keyObject)
{
throw new ArgumentNullException("key");
}
else
{
if(keyObject is TKey)
return (TKey)keyObject;
}
throw new ArgumentException("'key' must be of type " + _keyTypeName, "key");
}

private static TValue ConvertToValueType(object value)
{
if(null == value)
{
if(_valueTypeIsReferenceType)
return default(TValue);
else
throw new ArgumentNullException("value");
}
else
{
if(value is TValue)
return (TValue)value;
}
throw new ArgumentException("'value' must be of type " + _valueTypeName, "value");
}

private Dictionary<TKey, TValue> Dictionary
{
get
{
if(null == _dictionary)
{
_dictionary = new Dictionary<TKey, TValue>(_initialCapacity, _comparer);
}
return _dictionary;
}
}

private List<KeyValuePair<TKey, TValue>> List
{
get
{
if(null == _list)
{
_list = new List<KeyValuePair<TKey, TValue>>(_initialCapacity);
}
return _list;
}
}

IDictionaryEnumerator IOrderedDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}

IDictionaryEnumerator IDictionary.GetEnumerator()
{
return Dictionary.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey,TValue>>.GetEnumerator()
{
return List.GetEnumerator();
}

public void Insert(int index, TKey key, TValue value)
{
if(index > Count || index < 0)
throw new ArgumentOutOfRangeException("index");

Dictionary.Add(key, value);
List.Insert(index, new KeyValuePair<TKey, TValue>(key, value));
}

void IOrderedDictionary.Insert(int index, object key, object value)
{
Insert(index, ConvertToKeyType(key), ConvertToValueType(value));
}

public void RemoveAt(int index)
{
if(index >= Count || index < 0)
throw new ArgumentOutOfRangeException("index", "'index' must be non-negative and less than the size of the collection");

TKey key = List[index].Key;

List.RemoveAt(index);
Dictionary.Remove(key);
}

public TValue this[int index]
{
get
{
return List[index].Value;
}

set
{
if(index >= Count || index < 0)
throw new ArgumentOutOfRangeException("index", "'index' must be non-negative and less than the size of the collection");

TKey key = List[index].Key;

List[index] = new KeyValuePair<TKey, TValue>(key, value);
Dictionary[key] = value;
}
}

object IOrderedDictionary.this[int index]
{
get
{
return this[index];
}

set
{
this[index] = ConvertToValueType(value);
}
}

void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
Add(key, value);
}

public int Add(TKey key, TValue value)
{
Dictionary.Add(key, value);
List.Add(new KeyValuePair<TKey,TValue>(key, value));
return Count - 1;
}

void IDictionary.Add(object key, object value)
{
Add(ConvertToKeyType(key), ConvertToValueType(value));
}

public void Clear()
{
Dictionary.Clear();
List.Clear();
}

public bool ContainsKey(TKey key)
{
return Dictionary.ContainsKey(key);
}

bool IDictionary.Contains(object key)
{
return ContainsKey(ConvertToKeyType(key));
}

bool IDictionary.IsFixedSize
{
get
{
return false;
}
}

public bool IsReadOnly
{
get
{
return false;
}
}

ICollection IDictionary.Keys
{
get
{
return (ICollection)Keys;
}
}

public int IndexOfKey(TKey key)
{
if(null == key)
throw new ArgumentNullException("key");

for(int index = 0; index < List.Count; index++)
{
KeyValuePair<TKey, TValue> entry = List[index];
TKey next = entry.Key;
if(null != _comparer)
{
if(_comparer.Equals(next, key))
{
return index;
}
}
else if(next.Equals(key))
{
return index;
}
}

return -1;
}

public bool Remove(TKey key)
{
if(null == key)
throw new ArgumentNullException("key");

int index = IndexOfKey(key);
if(index >= 0)
{
if(Dictionary.Remove(key))
{
List.RemoveAt(index);
return true;
}
}
return false;
}

void IDictionary.Remove(object key)
{
Remove(ConvertToKeyType(key));
}

ICollection IDictionary.Values
{
get
{
return (ICollection)Values;
}
}

public TValue this[TKey key]
{
get
{
return Dictionary[key];
}
set
{
if(Dictionary.ContainsKey(key))
{
Dictionary[key] = value;
List[IndexOfKey(key)] = new KeyValuePair<TKey, TValue>(key, value);
}
else
{
Add(key, value);
}
}
}

object IDictionary.this[object key]
{
get
{
return this[ConvertToKeyType(key)];
}
set
{
this[ConvertToKeyType(key)] = ConvertToValueType(value);
}
}

void ICollection.CopyTo(Array array, int index)
{
((ICollection)List).CopyTo(array, index);
}

public int Count
{
get
{
return List.Count;
}
}

bool ICollection.IsSynchronized
{
get
{
return false;
}
}

object ICollection.SyncRoot
{
get
{
if(this._syncRoot == null)
{
System.Threading.Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
}
return this._syncRoot;
}
}

public ICollection<TKey> Keys
{
get
{
return Dictionary.Keys;
}
}

public bool TryGetValue(TKey key, out TValue value)
{
return Dictionary.TryGetValue(key, out value);
}

public ICollection<TValue> Values
{
get
{
return Dictionary.Values;
}
}

void ICollection<KeyValuePair<TKey,TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}

bool ICollection<KeyValuePair<TKey,TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey,TValue>>)Dictionary).Contains(item);
}

void ICollection<KeyValuePair<TKey,TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey,TValue>>)Dictionary).CopyTo(array, arrayIndex);
}

bool ICollection<KeyValuePair<TKey,TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
return Remove(item.Key);
}
}
}

这就是上面给定的 OrderedDictionary 的使用方式:

IRoomSettingsModel.cs

namespace TurboLabz.Game
{
public interface IRoomSettingsModel
{
IOrderedDictionary<string, RoomInfo> settings { get; set; }
}
}

RoomSettingsModel.cs

namespace TurboLabz.Game
{
public class RoomSettingsModel : IRoomSettingsModel
{
public IOrderedDictionary<string, RoomInfo> settings { get; set; }

public RoomSettingsModel()
{
settings = new OrderedDictionary<string, RoomInfo>();
}
}

public struct RoomInfo
{
public string id;
public long gameDuration;
public long prize;
}
}

GSService.cs

namespace TurboLabz.Game
{
public class SomeService
{
public IRoomSettingsModel roomSettingsModel = new RoomSettingsModel();

public void ReadModel()
{
foreach (KeyValuePair<string, RoomInfo> room in roomSettingsModel.settings)
{
RoomInfo roomInfo = room.Value;
Debug.Log(roomInfo.id);
}
}
}
}

为了保密,我在这里稍微更改了代码,但总的来说它应该传达了这个想法。上面用法中最重要的语句是foreach (KeyValuePair<string, RoomInfo> room in roomSettingsModel.settings)这是警告的来源。正是在这一行中,我认为编译器对哪个 GetEnumerator() 感到困惑。调用方法。

首先,这真的是问题所在吗?其次,我该如何解决这个问题?

最佳答案

我试着按照你所做的去做,但它是一堆嵌套接口(interface)。

如果你在每个 GetEnumerator() 中放置断点在OrderedDictionary ,您可能会发现它没有调用您期望的枚举器。

我认为问题在于尝试实现非通用 IOrderedDictionary界面以及IDictionary<TKey, TValue> .

如果你想要泛型,为什么你需要保持与非泛型的兼容性IOrderedDictionary

如果您遵循 (F12) IOrderedDictionary 的继承轨迹, 它继承了 IDictionary , ICollection , IEnumerable .

然后IDictionary<TKey, TValue>继承自 ICollection<KeyValuePair<TKey, TValue>> , IEnumerable<KeyValuePair<TKey, TValue>> , IEnumerable .

我不太确定您的所有要求是什么,但我会放弃您不需要支持的任何接口(interface)。不要提供您不需要的代码功能。

这不完全是您造成的,但它是尝试支持带有大量自身包袱的多个接口(interface)的结果。

根据你的问题,我只支持IDictionary<TKey, TValue> & IList<T> .

还有他们的行李 ;)

对于那些对 KeyedCollection 感到好奇的人,这里有一个实现了 @Mubeen 在他的代码中实现的大部分功能。这还没有经过全面测试,所以如果您使用它,请不要只是复制->粘贴。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

namespace TurboLabz.Game
{
public class GenericComparer<TKey> : IComparer<TKey>
{
public static GenericComparer<TKey> CreateComparer(Func<TKey, TKey, int> comparer)
{
return new GenericComparer<TKey>(comparer);
}

internal GenericComparer(Func<TKey, TKey, int> comparer)
{
Comparer = comparer;
}

private Func<TKey, TKey, int> Comparer { get; set; }

public int Compare(TKey x, TKey y)
{
return Comparer(x, y);
}
}

public class OrderedDictionaryKC<TKey, TValue> : KeyedCollection<TKey,KeyValuePair<TKey, TValue>>
{
public OrderedDictionaryKC()
{ }

public OrderedDictionaryKC(IEnumerable<KeyValuePair<TKey, TValue>> collection)
{
if (collection != null)
{
foreach (KeyValuePair<TKey, TValue> item in collection)
{
base.Add(item);
}
}
}

public OrderedDictionaryKC(IDictionary<TKey, TValue> dictionary) : this((IEnumerable<KeyValuePair<TKey, TValue>>)dictionary)
{ }

public ICollection<TKey> Keys
{
get
{
return base.Dictionary.Keys;
}
}

public ICollection<KeyValuePair<TKey, TValue>> Values
{
get
{
return base.Dictionary.Values;
}
}

public void Add(TKey key, TValue value)
{
if (key == null)
{
throw new ArgumentNullException("key");
}

base.Add(new KeyValuePair<TKey, TValue>(key, value));
}

public bool ContainsKey(TKey key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}

return base.Dictionary.ContainsKey(key);
}

public bool TryGetValue(TKey key, out TValue value)
{
KeyValuePair<TKey, TValue> outValue;
var result= base.Dictionary.TryGetValue(key, out outValue);
value = outValue.Value;

return result;
}

protected override TKey GetKeyForItem(KeyValuePair<TKey, TValue> item)
{
return item.Key;
}
}
}

关于c# - C# 中 OrderDictionary 的通用实现显示不明确的方法警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45416379/

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