C# 转换字典<;字符串,对象>;到类和子类

C# 转换字典<;字符串,对象>;到类和子类,c#,dictionary,C#,Dictionary,如何递归地将字典转换为类和子类? 这些是我的课程: public class UiItem { public string id { get; set; } public string text { get; set; } public Rect rect { get; set; } } public class Rect { public int height { get; set; } public int width { get; set; }

如何递归地将字典转换为类和子类? 这些是我的课程:

public class UiItem
{
    public string id { get; set; }
    public string text { get; set; }
    public Rect rect { get; set; } 
}

public class Rect
{
    public int height { get; set; }
    public int width { get; set; }
    public int y { get; set; }
    public int x { get; set; }
}
我写了这个,但我不知道如何在类
UiItem
中创建对象
Rect

public static T GetObject<T>(this Dictionary<string, object> dict)
    {
        Type type = typeof(T);
        var obj = Activator.CreateInstance(type);

        foreach (var kv in dict)
        {
            var prop = type.GetProperty(kv.Key);
            object value = kv.Value;
            if (kv.Value.GetType() == typeof(Dictionary<string, object>))
            {
                value = GetObject<_???_>((Dictionary<string, object>) value) // <= This line
            }

            if(prop == null) continue;
            prop.SetValue(obj, value, null);
        }
        return (T)obj;
    }
publicstatict GetObject(此字典dict)
{
类型=类型(T);
var obj=Activator.CreateInstance(类型);
foreach(以dict为单位的var kv)
{
var prop=类型GetProperty(千伏键);
对象值=千伏值;
if(kv.Value.GetType()==typeof(字典))
{

value=GetObject((Dictionary)value)//最简单的方法是将类型作为参数传递,而不是使用泛型方法。这是:

public static Object GetObject(this Dictionary<string, object> dict, Type type)
    {
        var obj = Activator.CreateInstance(type);

        foreach (var kv in dict)
        {
            var prop = type.GetProperty(kv.Key);
            if(prop == null) continue;

            object value = kv.Value;
            if (value is Dictionary<string, object>)
            {
                value = GetObject((Dictionary<string, object>) value, prop.PropertyType); // <= This line
            }

            prop.SetValue(obj, value, null);
        }
        return obj;
    }
公共静态对象GetObject(此字典dict dict,类型)
{
var obj=Activator.CreateInstance(类型);
foreach(以dict为单位的var kv)
{
var prop=类型GetProperty(千伏键);
如果(prop==null)继续;
对象值=千伏值;
if(值为字典)
{

value=GetObject((Dictionary)value,prop.PropertyType);//灵感来源于@Dan Bryant的答案,但就我而言,我的字典有ILists属性,所以我在这里告诉你我是如何做到的

public static T ToClass<T>(Dictionary<string, object> dict)
{
   return (T)ConverToClass(dict, typeof(T));
}

  
    private static object ConverToClass(Dictionary<string, object> dic, Type classToUse)
    {
        Type type = classToUse;
        var obj = Activator.CreateInstance(type);

        foreach (var item in dic)
        {
            var property = type.GetProperty(item.Key);
            if (property == null) continue;

            var value = item.Value;
            if (value is Dictionary<string, object> && !property.PropertyType.FullName.Contains("Generic.IList"))
            {
                property.SetValue(obj, ConverToClass((Dictionary<string, object>)(item.Value), property.PropertyType));
                continue;
            }
            if (property.PropertyType.FullName.Contains("Generic.IList"))
            {
                var subClassTouse = property.PropertyType.GetGenericArguments()[0];

                Type genericListType = typeof(List<>);
                Type concreteListType = genericListType.MakeGenericType(subClassTouse);
                var list = (IList)Activator.CreateInstance(concreteListType, new object[] { });

                var values = (Dictionary<string, object>)dic[item.Key];

                foreach (var itemClass in values)
                {
                    list.Add(ConverToClass((Dictionary<string, object>)itemClass.Value, subClassTouse));
                }
                property.SetValue(obj, list);
                continue;
            }
            property.SetValue(obj, item.Value);
        }

        return obj;
    }
publicstatict类(Dictionary dict)
{
返回(T)ConverToClass(dict,typeof(T));
}
私有静态对象ConverToClass(字典dic,类型classToUse)
{
类型=classtuse;
var obj=Activator.CreateInstance(类型);
foreach(dic中的var项目)
{
var属性=type.GetProperty(item.Key);
如果(property==null)继续;
var值=项目价值;
if(值为Dictionary&!property.PropertyType.FullName.Contains(“Generic.IList”))
{
SetValue(obj,ConverToClass((Dictionary)(item.Value),property.PropertyType));
继续;
}
if(property.PropertyType.FullName.Contains(“Generic.IList”))
{
var subclass=property.PropertyType.GetGenericArguments()[0];
类型genericListType=typeof(列表);
类型concreteListType=genericListType.MakeGenericType(子类);
var list=(IList)Activator.CreateInstance(concreteListType,新对象[]{});
var值=(字典)dic[item.Key];
foreach(值中的var itemClass)
{
添加(ConverToClass((Dictionary)itemClass.Value,subclass));
}
设置值(对象,列表);
继续;
}
属性.SetValue(对象,项.Value);
}
返回obj;
}

您的对象结构
n
levels deep吗?如果它只有两个levels deep,为什么不使用两个类型参数?这太好了!我确实需要添加一些额外的代码来处理可空性。
public static T ToClass<T>(Dictionary<string, object> dict)
{
   return (T)ConverToClass(dict, typeof(T));
}

  
    private static object ConverToClass(Dictionary<string, object> dic, Type classToUse)
    {
        Type type = classToUse;
        var obj = Activator.CreateInstance(type);

        foreach (var item in dic)
        {
            var property = type.GetProperty(item.Key);
            if (property == null) continue;

            var value = item.Value;
            if (value is Dictionary<string, object> && !property.PropertyType.FullName.Contains("Generic.IList"))
            {
                property.SetValue(obj, ConverToClass((Dictionary<string, object>)(item.Value), property.PropertyType));
                continue;
            }
            if (property.PropertyType.FullName.Contains("Generic.IList"))
            {
                var subClassTouse = property.PropertyType.GetGenericArguments()[0];

                Type genericListType = typeof(List<>);
                Type concreteListType = genericListType.MakeGenericType(subClassTouse);
                var list = (IList)Activator.CreateInstance(concreteListType, new object[] { });

                var values = (Dictionary<string, object>)dic[item.Key];

                foreach (var itemClass in values)
                {
                    list.Add(ConverToClass((Dictionary<string, object>)itemClass.Value, subClassTouse));
                }
                property.SetValue(obj, list);
                continue;
            }
            property.SetValue(obj, item.Value);
        }

        return obj;
    }