2010-02-02 13 views
10

मैं एक एपीआई का उपयोग कर रहा हूं जो Dictionary<string, string> के रूप में एक कुंजी-मूल्य संग्रह देता है। मुझे इसे Dictionary<string, object> में परिवर्तित करने की आवश्यकता है। मुझे एहसास है कि प्रत्येक कुंजी-मूल्य जोड़ी के माध्यम से "मैन्युअल" लूपिंग के बिना इस रूपांतरण/मानचित्रण को करने का कोई तरीका होना चाहिए, लेकिन गूगलिंग या सी # ऑब्जेक्ट संदर्भ तुरंत समाधान नहीं मिला।शब्दकोश <स्ट्रिंग, स्ट्रिंग> को एक शब्दकोश <string, object> में कनवर्ट करने का सबसे कम तरीका क्या है?

उत्तर

22

निम्नलिखित इस विस्तार विधि

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

+1, हालांकि यह ध्यान देने योग्य है कि ओपी जिस तरह से बचाना चाहता है उससे कहीं भी "दुबला" नहीं है - यह सिर्फ एक सुंदर चमकदार वाक्यविन्यास के पीछे छिपा हुआ है। –

+0

@Rex, सच है, लेकिन यह ओपी द्वारा पूछे गए मैनुअल लूप से बचाता है। – JaredPar

+0

"दुबला" अलग-अलग चीजों के लिए बढ़ाया जा सकता है। मेरे मामले में मैं बस दिन बचाने के लिए एक त्वरित समाधान की तलाश में था। इस उत्तर के लिए धन्यवाद। –

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(); 
3

कोई पाशन, नक्शे एक Dictionary{T, U}तक लगातार समय में:

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{C, D} करने के लिए एक Dictionary{A, B} नक्शे इस वर्ग के सामान्यीकरण कर सकते हैं।

संबंधित मुद्दे