64

如何Dictionary<TKey, TValue>在 C# 中将动态对象转换为 a 我该怎么办?

public static void MyMethod(object obj)
{
    if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
    {
        // My object is a dictionary, casting the object:
        // (Dictionary<string, string>) obj;
        // causes error ...
    }
    else
    {
        // My object is not a dictionary
    }
}
4

18 回答 18

120

上面的答案都很酷。我发现 json 序列化对象和反序列化为字典很容易。

var json = JsonConvert.SerializeObject(obj);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

我不知道性能是如何受到影响的,但这更容易阅读。您也可以将其包装在一个函数中。

public static Dictionary<string, TValue> ToDictionary<TValue>(object obj)
{       
    var json = JsonConvert.SerializeObject(obj);
    var dictionary = JsonConvert.DeserializeObject<Dictionary<string, TValue>>(json);   
    return dictionary;
}

像这样使用:

var obj = new { foo = 12345, boo = true };
var dictionary = ToDictionary<string>(obj);
于 2017-10-27T22:05:34.880 回答
52

我使用这个助手:

public static class ObjectToDictionaryHelper
{
    public static IDictionary<string, object> ToDictionary(this object source)
    {
        return source.ToDictionary<object>();
    }

    public static IDictionary<string, T> ToDictionary<T>(this object source)
    {
        if (source == null)
            ThrowExceptionWhenSourceArgumentIsNull();

        var dictionary = new Dictionary<string, T>();
        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
            AddPropertyToDictionary<T>(property, source, dictionary);
        return dictionary;
    }

    private static void AddPropertyToDictionary<T>(PropertyDescriptor property, object source, Dictionary<string, T> dictionary)
    {
        object value = property.GetValue(source);
        if (IsOfType<T>(value))
            dictionary.Add(property.Name, (T)value);
    }

    private static bool IsOfType<T>(object value)
    {
        return value is T;
    }

    private static void ThrowExceptionWhenSourceArgumentIsNull()
    {
        throw new ArgumentNullException("source", "Unable to convert object to a dictionary. The source object is null.");
    }
}

用法只是调用.ToDictionary()一个对象

希望能帮助到你。

于 2014-04-22T16:56:40.220 回答
25
    public static KeyValuePair<object, object > Cast<K, V>(this KeyValuePair<K, V> kvp)
    {
        return new KeyValuePair<object, object>(kvp.Key, kvp.Value);
    }

    public static KeyValuePair<T, V> CastFrom<T, V>(Object obj)
    {
        return (KeyValuePair<T, V>) obj;
    }

    public static KeyValuePair<object , object > CastFrom(Object obj)
    {
        var type = obj.GetType();
        if (type.IsGenericType)
        {
            if (type == typeof (KeyValuePair<,>))
            {
                var key = type.GetProperty("Key");
                var value = type.GetProperty("Value");
                var keyObj = key.GetValue(obj, null);
                var valueObj = value.GetValue(obj, null);
                return new KeyValuePair<object, object>(keyObj, valueObj);
            }
        }
        throw new ArgumentException(" ### -> public static KeyValuePair<object , object > CastFrom(Object obj) : Error : obj argument must be KeyValuePair<,>");
    }

从OP:

我决定让我的 obj 始终保持动态,而不是转换我的整个字典。当我稍后使用 foreach 访问我的字典的键和值时,我使用 foreach(obj.Keys 中的动态键)并将键和值简单地转换为字符串。

于 2012-11-28T10:21:56.380 回答
17

另一种选择是使用 NewtonSoft.JSON。

var dictionary = JObject.FromObject(anObject).ToObject<Dictionary<string, object>>();
于 2020-10-28T15:07:30.650 回答
7

这应该工作:

对于数字、字符串、日期等:

    public static void MyMethod(object obj)
    {
        if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
        {
            IDictionary idict = (IDictionary)obj;

            Dictionary<string, string> newDict = new Dictionary<string, string>();
            foreach (object key in idict.Keys)
            {
                newDict.Add(key.ToString(), idict[key].ToString());
            }
        }
        else
        {
            // My object is not a dictionary
        }
    }

如果您的字典还包含其他一些对象:

    public static void MyMethod(object obj)
    {
        if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
        {
            IDictionary idict = (IDictionary)obj;
            Dictionary<string, string> newDict = new Dictionary<string, string>();

            foreach (object key in idict.Keys)
            {
                newDict.Add(objToString(key), objToString(idict[key]));
            }
        }
        else
        {
            // My object is not a dictionary
        }
    }

    private static string objToString(object obj)
    {
        string str = "";
        if (obj.GetType().FullName == "System.String")
        {
            str = (string)obj;
        }
        else if (obj.GetType().FullName == "test.Testclass")
        {
            TestClass c = (TestClass)obj;
            str = c.Info;
        }
        return str;
    }
于 2012-07-20T10:06:09.817 回答
7

如果您介意使用 LINQ 表达式;

public static Dictionary<string, object> ConvertFromObjectToDictionary(object arg)
{
     return arg.GetType().GetProperties().ToDictionary(property => property.Name, property => property.GetValue(arg));
}
于 2020-08-19T08:31:25.500 回答
3

Assuming key can only be a string but value can be anything try this

public static Dictionary<TKey, TValue> MyMethod<TKey, TValue>(object obj)
{
    if (obj is Dictionary<TKey, TValue> stringDictionary)
    {
        return stringDictionary;
    }

    if (obj is IDictionary baseDictionary)
    {
        var dictionary = new Dictionary<TKey, TValue>();
        foreach (DictionaryEntry keyValue in baseDictionary)
        {
            if (!(keyValue.Value is TValue))
            {
                // value is not TKey. perhaps throw an exception
                return null;
            }
            if (!(keyValue.Key is TKey))
            {
                // value is not TValue. perhaps throw an exception
                return null;
            }

            dictionary.Add((TKey)keyValue.Key, (TValue)keyValue.Value);
        }
        return dictionary;
    }
    // object is not a dictionary. perhaps throw an exception
    return null;
}
于 2012-07-20T11:45:16.347 回答
3
   public static void MyMethod(object obj){
   Dictionary<string, string> dicEditdata = data as Dictionary<string, string>;
   string abc=dicEditdata["id"].ToString();} 

假设---如果您在调试时将光标放在对象(obj)上,并且如果您得到一个具有该值的对象,{['id':'ID1003']} 那么您可以将该值用作

string abc=dicEditdata["id"].ToString(); 
于 2014-08-01T12:02:27.077 回答
2

我做过这样的事情并且为我工作。

使用 System.ComponentModel;

var dictionary = new Dictionary<string, string>();

foreach (var propDesc in TypeDescriptor.GetProperties(Obj))
{
    if (!string.IsNullOrEmpty(propDesc.GetValue(Obj)))
    {
        dictionary.Add(propDesc.Name, propDesc.GetValue(Obj));
    }
}

此外,这里还有另一种替代和创新的解决方案。

var dictionary = new System.Web.Routing.RouteValueDictionary(Obj);
于 2021-03-13T00:52:04.477 回答
1

此代码可以安全地将对象转换为字典(前提是源对象来自字典):

    private static Dictionary<TKey, TValue> ObjectToDictionary<TKey, TValue>(object source)
    {
        Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>();

        TKey[] keys = { };
        TValue[] values = { };

        bool outLoopingKeys = false, outLoopingValues = false;

        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
        {
            object value = property.GetValue(source);
            if (value is Dictionary<TKey, TValue>.KeyCollection)
            {
                keys = ((Dictionary<TKey, TValue>.KeyCollection)value).ToArray();
                outLoopingKeys = true;
            }
            if (value is Dictionary<TKey, TValue>.ValueCollection)
            {
                values = ((Dictionary<TKey, TValue>.ValueCollection)value).ToArray();
                outLoopingValues = true;
            }
            if(outLoopingKeys & outLoopingValues)
            {
                break;
            }
        }

        for (int i = 0; i < keys.Length; i++)
        {
            result.Add(keys[i], values[i]);
        }

        return result;
    }
于 2018-10-24T05:04:22.603 回答
0

我使用这个简单的方法:

public Dictionary<string, string> objToDict(XYZ.ObjectCollection objs) {
    var dict = new Dictionary<string, string>();
    foreach (KeyValuePair<string, string> each in objs){
        dict.Add(each.Key, each.Value);
    }
    return dict;
}
于 2018-02-17T15:45:07.740 回答
0

你可以使用这个:

Dictionary<object,object> mydic = ((IEnumerable)obj).Cast<object>().ToList().ToDictionary(px => px.GetType().GetProperty("Key").GetValue(px), pv => pv.GetType().GetProperty("Value").GetValue(pv));
于 2019-10-31T05:15:19.313 回答
0

您可以创建一个通用扩展方法,然后在对象上使用它,例如:

public static class Extensions
{
    public static KeyValuePair<TKey, TValue> ToKeyValuePair<TKey, TValue>(this Object obj)
    {
        // if obj is null throws exception
        Contract.Requires(obj != null);

        // gets the type of the obj parameter
        var type = obj.GetType();
        // checks if obj is of type KeyValuePair
        if (type.IsGenericType && type == typeof(KeyValuePair<TKey, TValue>))
        {

            return new KeyValuePair<TKey, TValue>(
                                                    (TKey)type.GetProperty("Key").GetValue(obj, null), 
                                                    (TValue)type.GetProperty("Value").GetValue(obj, null)
                                                 );

        }
        // if obj type does not match KeyValuePair throw exception
        throw new ArgumentException($"obj argument must be of type KeyValuePair<{typeof(TKey).FullName},{typeof(TValue).FullName}>");   
 }

用法如下:

KeyValuePair<string,long> kvp = obj.ToKeyValuePair<string,long>();
于 2016-11-28T11:06:35.677 回答
0

我希望这可以工作:)

    //  obj = new { a = "string", b = 0, c = true };
    static Dictionary<string, object> ToDictionary(object obj)
    {
        int i = 0;
        var props = obj.GetType().GetProperties();
        return props.ToDictionary(k => props[i].Name, v => props[i++].GetValue(obj));
    }
于 2022-03-01T00:02:44.303 回答
0

这种方式将对象数组转换为 Dictionary<string, object> 列表覆盖

object[] a = new object[2];
var x = a.Select(f => (Dictionary<string, object>)f).ToList();

这种方式将单个对象转换为 Dictionary<string, object>

object a = new object;
var x = (Dictionary<string, object>)a;
于 2022-02-06T16:59:25.667 回答
-1

据我了解,您不确定键和值是什么,但您想将它们转换为字符串?

也许这可以工作:

public static void MyMethod(object obj)
{
  var iDict = obj as IDictionary;
  if (iDict != null)
  {
    var dictStrStr = iDict.Cast<DictionaryEntry>()
      .ToDictionary(de => de.Key.ToString(), de => de.Value.ToString());

    // use your dictStrStr        
  }
  else
  {
    // My object is not an IDictionary
  }
}
于 2012-07-20T10:10:41.317 回答
-1
object parsedData = se.Deserialize(reader);
System.Collections.IEnumerable stksEnum = parsedData as System.Collections.IEnumerable;

然后就能列举出来了!

于 2014-07-18T17:39:09.023 回答
-2

简单的方法:

public IDictionary<T, V> toDictionary<T, V>(Object objAttached)
{
    var dicCurrent = new Dictionary<T, V>();
    foreach (DictionaryEntry dicData in (objAttached as IDictionary))
    {
        dicCurrent.Add((T)dicData.Key, (V)dicData.Value);
    }
    return dicCurrent;
}
于 2016-09-15T03:56:30.687 回答