我正在使用一個返回鍵值集合的API作爲Dictionary<string, string>
。我需要將其轉換爲Dictionary<string, object>
。我有一種感覺,應該有一種方法可以在不通過每個鍵值對「手動」循環的情況下執行此轉換/映射,但Google或者C#對象引用不會立即產生解決方案。將字典<字符串,字符串>轉換爲字典<字符串,對象>的最簡單方法是什麼?
10
A
回答
22
請嘗試以下
var newMap = oldMap.ToDictionary(pair => pair.Key, pair=>(object)pair.Value);
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}
到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}
。
相關問題
- 1. 將字典<對象,字符串>轉換爲字典<字符串,字符串>
- 2. 如何將字典<字符串,對象>轉換爲字典<字符串,字典<字符串,字符串>>>
- 3. 轉換複雜對象字典<字符串,字符串>
- 4. 將字典<字符串,字符串>轉換爲數組
- 5. C#:字典的轉換<字符串,字符串>到詞典<字符串,字典<字符串,字符串>>
- 6. 爲什麼我無法創建字典<字符串,字典<字符串,字符串>>?
- 7. 在c#中將字典轉換爲字典中的字符串<字符串,字典<string,int>>?
- 8. 算法來填充詞典<字符串,字典<字符串,字符串>>鍵入對象
- 9. 使用一個字典<串,對象>作爲字典<字符串,字典<字符串,字符串>>
- 10. 合併兩個字典字典<字符串,字典<字符串,對象>>
- 11. 詞典與字典<字符串,字符串>
- 12. 字典<字符串,字符串>到詞典<控制,對象>使用的IEnumerable <T>。選擇()
- 13. 轉換詞典<字符串,字符串>到XML
- 14. XSLT變換字典<字符串,字符串>
- 15. 簡單的方法來將字典<字符串,字符串>轉換爲xml,反之亦然
- 16. 如何將項目添加到詞典<字符串,字典<字符串,對象>>?
- 17. 將字典<字符串:任何?>轉換爲NSData
- 18. LINQ的返回的IEnumerable <字典<字符串,字符串>>
- 19. 如何綁定到詞典<字符串,字典<字符串,[objectArray]>
- 20. 從字典<字符串,字符串>
- 21. 比較2字典<字符串,字符串>實例
- 22. LINQ - 轉彎列表<string>到詞典<字符串,字符串>
- 23. Java的轉換{字符串,字符串} []地圖<字符串,字符串[]>
- 24. Java的地圖<字符串,地圖<字符串,對象>>轉換爲字符串和背部
- 25. 無法隱式轉換 'System.Collections.Generic.Dictionary <字符串,System.Collections.Generic.List <string>>' 到 '系統... <字符串,字符串>'
- 26. 在列表<字典<字符串,字符串>>其中字典值是日期爲字符串選擇最小密鑰值對
- 27. 序列化對象成扁平字典<字符串,字符串>
- 28. 演員詞典<字符串,類>到的IDictionary <字符串,接口>
- 29. 將字符串轉換爲<S3Uri>
- 30. 什麼是Java correponding收集到詞典<字符串,字符串>
+1,但值得注意的是,這並不比OP想要避免的方式「更精簡」 - 它只是隱藏在一個非常閃亮的語法之後。 – 2010-02-02 23:14:02
@Rex,true,但它避免了OP所要求的手動循環。 – JaredPar 2010-02-02 23:17:20
「精益」可以延伸到意味着不同的東西。就我而言,我只是尋找一個快速解決方案來節省一天的時間。感謝這個答案。 – 2010-02-03 00:46:20