简体   繁体   English

转换字典的最简单方法是什么? <string, string> 到字典 <string, object> ?

[英]What is the leanest way to convert a Dictionary<string, string> to a Dictionary<string, object>?

I'm using an API that returns a key-value collection as a Dictionary<string, string> . 我正在使用一个API,它将键值集合作为Dictionary<string, string> I need to convert that to a Dictionary<string, object> . 我需要将其转换为Dictionary<string, object> I have a sense that there should be a way to do this conversion/mapping without "manually" looping through each key-value pair, but Googling or the C# object reference didn't immediately yield a solution. 我有一种感觉,应该有一种方法来做这个转换/映射而不“手动”循环每个键值对,但谷歌搜索或C#对象引用没有立即产生解决方案。

请尝试以下方法

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

No looping, maps a Dictionary{T, U} to Dictionary{T, object} in constant time: 没有循环,在恒定时间内将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
}

With a few more generic params, you can generalize this class further so that it maps a Dictionary{A, B} to a Dictionary{C, D} . 通过一些更通用的参数,您可以进一步推广此类,以便将Dictionary{A, B}映射到Dictionary{C, D}

You can use this extension method: 您可以使用此扩展方法:

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;
    }
}

You use it like: 你使用它像:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换字典 <object, string> 到字典 <string, string> - Convert Dictionary<object, string> to Dictionary<string, string> 有没有一种简单的方法可以将对象属性转换为字典<string, string> - Is there an easy way to convert object properties to a dictionary<string, string> 如何转换字典 <string, object> 到字典 <string, string> 在C#中 - How to convert Dictionary<string, object> to Dictionary<string, string> in c# 转换清单 <Dictionary<string, string> &gt;到字典 <string, Dictionary<string, string> &gt; - convert List<Dictionary<string, string>> to Dictionary<string, Dictionary<string, string>> 转换字典 <string,object> 到字典 <string,class> 在C#中 - Convert Dictionary<string,object> to Dictionary<string,class> in c# C#转换字典 <string, List<> &gt;到字典 <string, object> - c# Convert Dictionary<string, List<>> to Dictionary<string, object> 将字典对象转换为Json字符串 - Convert dictionary object into Json string 迭代字典的最佳方法 <string, Dictionary<string, object> &gt;在NVelocity - Best way to iterate Dictionary<string, Dictionary<string, object>> in NVelocity 使用字典 <string, object> 作为字典 <string, Dictionary<string, string> &gt; - Using an Dictionary<string, object> as Dictionary<string, Dictionary<string, string>> 有没有一种方法可以序列化Dictionary <string, string> 有一个内在的对象? - Is there a way to serialize Dictionary<string, string> with an inner object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM