本文介绍了映射对象字典,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有优雅快速的方法来映射对象字典,反之亦然?

例:

 的IDictionary<字符串,对象> A =新字典<字符串,对象>();
一[ID] = 1;
一[名称] =艾哈迈德;
// .....
 

变为

  SomeClass的B =新SomeClass的();
b.Id = 1;
b.Name =艾哈迈德;
// ..........
 

解决方案

使用一些反思和仿制药的两种方法扩展,你可以做到这一点。

右键,其他人却大都相同的解决方案,但这种使用更少的反射是更多的性能,明智和方式更具有可读性:

 公共静态类ObjectExtensions
{
    公共静态牛逼ToObject< T>(这IDictionary的<字符串,对象>源)
        其中T:类,新的()
    {
            牛逼someObject =新T();
            键入someObjectType = someObject.GetType();

            的foreach(KeyValuePair<字符串,对象>中源项)
            {
                someObjectType.GetProperty(item.Key).SetValue(someObject,item.Value,NULL);
            }

            返回someObject;
    }

    公共静态的IDictionary<字符串,对象> AsDictionary(此对象源,的BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        返回source.GetType()。GetProperties中(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(源,空)
        );

    }
}

A级
{
    公共字符串为prop1
    {
        得到;
        组;
    }

    公众诠释Prop2
    {
        得到;
        组;
    }
}

类节目
{
    静态无效的主要(字串[] args)
    {
        字典<字符串,对象>词典=新词典<字符串,对象>();
        dictionary.Add(为prop1,世界,你好!);
        dictionary.Add(Prop2,3893);
        一个someObject = dictionary.ToObject< A>();

        IDictionary的<字符串,对象> objectBackToDictionary = someObject.AsDictionary();
    }
}
 

Are there any elegant quick way to map object to a dictionary and vice versa?

Example:

IDictionary<string,object> a = new Dictionary<string,object>();
a["Id"]=1;
a["Name"]="Ahmad";
// .....

becomes

SomeClass b = new SomeClass();
b.Id=1;
b.Name="Ahmad";
// ..........
解决方案

Using some reflection and generics in two methods extensions you can achieve that.

Right, others did mostly the same solution, but this uses less reflection which is more performance-wise and way more readable:

public static class ObjectExtensions
{
    public static T ToObject<T>(this IDictionary<string, object> source)
        where T : class, new()
    {
            T someObject = new T();
            Type someObjectType = someObject.GetType();

            foreach (KeyValuePair<string, object> item in source)
            {
                someObjectType.GetProperty(item.Key).SetValue(someObject, item.Value, null);
            }

            return someObject;
    }

    public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        return source.GetType().GetProperties(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source, null)
        );

    }
}

class A
{
    public string Prop1
    {
        get;
        set;
    }

    public int Prop2
    {
        get;
        set;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        dictionary.Add("Prop1", "hello world!");
        dictionary.Add("Prop2", 3893);
        A someObject = dictionary.ToObject<A>();

        IDictionary<string, object> objectBackToDictionary = someObject.AsDictionary();
    }
}

这篇关于映射对象字典,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 02:21