将Dictionary <string,string =“”>转换为Dictionary <string,object =“”>?</string,> </string,>的最简单方法是什么?

时间:2010-02-02 23:06:00

标签: c# dictionary casting

我正在使用一个将键值集合作为Dictionary<string, string>返回的API。我需要将其转换为Dictionary<string, object>。我有一种感觉,应该有一种方法来进行这种转换/映射,而无需“手动”循环遍历每个键值对,但谷歌搜索或C#对象引用没有立即产生解决方案。

3 个答案:

答案 0 :(得分:24)

尝试以下

var newMap = oldMap.ToDictionary(pair => pair.Key, pair=>(object)pair.Value);

答案 1 :(得分:3)

无循环,将Dictionary{T, U}映射到Dictionary{T, object}并保持恒定时间:

class DictionaryWrapper<T, U> : IDictionary<T, object>
{
    readonly Dictionary<T, U> inner;
    public DictionaryWrapper(Dictionary<T, U> wrapped)
    {
        this.inner = wrapped;
    }

    #region IDictionary<T,object> Members

    public void Add(T key, object value) { inner.Add(key, (U)value); }
    public bool ContainsKey(T key) { return inner.ContainsKey(key); }
    public ICollection<T> Keys { get { return inner.Keys; } }
    public bool Remove(T key) { return inner.Remove(key); }

    public bool TryGetValue(T key, out object value)
    {
        U temp;
        bool res = inner.TryGetValue(key, out temp);
        value = temp;
        return res;
    }

    public ICollection<object> Values { get { return inner.Values.Select(x => (object)x).ToArray(); } }

    public object this[T key]
    {
        get { return inner[key]; }
        set { inner[key] = (U)value; }
    }

    #endregion

    #region ICollection<KeyValuePair<T,object>> Members

    public void Add(KeyValuePair<T, object> item) { inner.Add(item.Key, (U)item.Value); }
    public void Clear() { inner.Clear(); }
    public bool Contains(KeyValuePair<T, object> item) { return inner.Contains(new KeyValuePair<T, U>(item.Key, (U)item.Value)); }
    public void CopyTo(KeyValuePair<T, object>[] array, int arrayIndex) { throw new NotImplementedException(); }
    public int Count { get { return inner.Count; } }
    public bool IsReadOnly { get { return false; } }
    public bool Remove(KeyValuePair<T, object> item) { return inner.Remove(item.Key); }

    #endregion

    #region IEnumerable<KeyValuePair<T,object>> Members

    public IEnumerator<KeyValuePair<T, object>> GetEnumerator()
    {
        foreach (var item in inner)
        {
            yield return new KeyValuePair<T, object>(item.Key, item.Value);
        }
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        foreach (var item in inner)
        {
            yield return new KeyValuePair<T, object>(item.Key, item.Value);
        }
    }

    #endregion
}

使用一些更通用的参数,您可以进一步推广此类,以便将Dictionary{A, B}映射到Dictionary{C, D}

答案 2 :(得分:0)

您可以使用此扩展方法:

public static class ObjectExtensions
{
    public static object GetPropertyValue(this object obj, string property)
    {
        return TypeDescriptor.GetProperties(obj)[property].GetValue(obj);
    }

    public static IDictionary<string, object> ToDictionary(this object obj)
    {
        IDictionary<string, object> result = new Dictionary<string, object>();
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
        foreach (PropertyDescriptor property in properties)
        {
            result.Add(property.Name, property.GetValue(obj));
        }
        return result;
    }
}

你可以像使用它一样:

new Dictionary<string, string>().ToDictionary();