当我不枚举IDictionary的键和值时';我不知道值的类型

本文关键字:我不知道 类型 键和值 枚举 IDictionary | 更新日期: 2023-09-27 18:00:15

我有一个对对象的引用。我知道它符合

IDictionary<string, T> 

对于某些类型T(它可能不符合普通IDictionary或IReadyOnlyDictionary)。关于T,我只知道它是从物体上下来的。如何获取它的密钥,并获取密钥的值?(我可以将值作为对象而不是T返回。我也可以从不学习T是什么。)

我想写但写不出来的东西是这样的:

public void SomeMethod(object reallyADict) { // reallyADict implements IDictionary<string, T>.
  foreach (string key in reallyADict.Keys) {
    object value = reallyADict[key];
    // . . .
  }
}

**

根据请求,下面是一个示例类。

using System;
using System.Collections.Generic;
using System.Collections;
namespace My.Collections
{
  public class WrappedDictionary: IDictionary<string, int>
  {
    public WrappedDictionary() {
      this.InnerDictionary = new Dictionary<string, int>{ {"one", 1}, {"two", 2 }};
    }
    private Dictionary<string, int> InnerDictionary { get; set;}
    private ICollection<KeyValuePair<string, int>> InnerCollection {
      get {
        return this.InnerDictionary;
      }
    }

    #region IDictionary implementation
    void IDictionary<string, int>.Add(string key, int value) {
      this.InnerDictionary.Add(key, value);
    }
    bool IDictionary<string, int>.ContainsKey(string key) {
      return this.InnerDictionary.ContainsKey(key);
    }
    bool IDictionary<string, int>.Remove(string key) {
      return this.InnerDictionary.Remove(key);
    }
    bool IDictionary<string, int>.TryGetValue(string key, out int value) {
      return this.InnerDictionary.TryGetValue(key, out value);
    }
    int IDictionary<string, int>.this[string index] {
      get {
        return this.InnerDictionary[index];
      }
      set {
        this.InnerDictionary[index] = value;
      }
    }
    ICollection<string> IDictionary<string, int>.Keys {
      get {
        return this.InnerDictionary.Keys;
      }
    }
    ICollection<int> IDictionary<string, int>.Values {
      get {
        return this.InnerDictionary.Values;
      }
    }
    #endregion
    #region ICollection implementation
    void ICollection<KeyValuePair<string, int>>.Add(KeyValuePair<string, int> item) {
      this.InnerCollection.Add(item);
    }
    void ICollection<KeyValuePair<string, int>>.Clear() {
      this.InnerDictionary.Clear();
    }
    bool ICollection<KeyValuePair<string, int>>.Contains(KeyValuePair<string, int> item) {
      return this.InnerCollection.Contains(item);
    }
    void ICollection<KeyValuePair<string, int>>.CopyTo(KeyValuePair<string, int>[] array, int arrayIndex) {
      this.InnerCollection.CopyTo(array, arrayIndex);
    }
    bool ICollection<KeyValuePair<string, int>>.Remove(KeyValuePair<string, int> item) {
      return this.InnerCollection.Remove(item);
    }
    int ICollection<KeyValuePair<string, int>>.Count {
      get {
        return this.InnerCollection.Count;
      }
    }
    bool ICollection<KeyValuePair<string, int>>.IsReadOnly {
      get {
        return this.InnerCollection.IsReadOnly;
      }
    }
    #endregion
    #region IEnumerable implementation
    IEnumerator<KeyValuePair<string, int>> IEnumerable<KeyValuePair<string, int>>.GetEnumerator() {
      return this.InnerCollection.GetEnumerator();
    }
    #endregion
    #region IEnumerable implementation
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
      return (this as IEnumerable).GetEnumerator();
    }
    #endregion
  }
}

当我不枚举IDictionary的键和值时';我不知道值的类型

不幸的是,您无法将reallyADict强制转换为类似Dictionary<string,T>的类型,因为您需要一个特定的类型。

Manfred关于使用这样的通用方法的评论

public IEnumerable<T> SomeMethod<T>(Dictionary<string, T> dict)

这也是我的做法。但你说你实际上只有object这本字典。

所以我用反射解决了这个问题:

public IEnumerable<object> SomeMethod(object reallyADict)
{
    Type genericInterface = reallyADict?.GetType().GetInterface("IDictionary`2");
    PropertyInfo propKeys = genericInterface?.GetProperty("Keys");
    if (propKeys?.GetMethod == null) yield break;
    IEnumerable<string> keys = (IEnumerable<string>)propKeys.GetValue(reallyADict);
    PropertyInfo propIndex = genericInterface.GetProperty("Item");
    if (propIndex?.GetMethod == null) yield break;
    foreach (string key in keys)
        yield return propIndex.GetMethod.Invoke(reallyADict, new object[] { key });
}

此方法从reallyDict(如果有)中获取Keys属性,并将其用作IEnumerable<string>

然后,它对所有这些键进行迭代,并使用底层字典的indexer属性来返回值。索引器属性的名称为Item