将对象映射到字典,反之亦然
是否有任何优雅的快速方法将对象映射到字典,反之亦然?
示例:
IDictionary<string,object> a = new Dictionary<string,object>();
a["Id"]=1;
a["Name"]="Ahmad";
// .....
变成
SomeClass b = new SomeClass();
b.Id=1;
b.Name="Ahmad";
// ..........
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";
// ..........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在两个扩展方法中使用一些反射和泛型,您可以实现这一目标。
是的,其他人做了大部分相同的解决方案,但这使用了更少的反射,这在性能上更明智并且更具可读性:
Using some reflection and generics in two extension methods 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:
首先使用 Newtonsoft 将字典转换为 JSON 字符串。
然后将 JSON 字符串反序列化为您的对象
Convert the Dictionary to JSON string first with Newtonsoft.
Then deserialize the JSON string to your object
似乎反射只在这里有帮助..我做了一个将对象转换为字典的小例子,反之亦然:
Seems reflection only help here.. I've done small example of converting object to dictionary and vise versa:
我强烈推荐 Castle DictionaryAdapter,很容易就是其中之一项目最保守的秘密。您只需要定义一个具有所需属性的接口,适配器将在一行代码中生成一个实现,实例化它,并将其值与您传入的字典同步。我使用它来对我的 AppSettings 进行强类型化一个 Web 项目:
请注意,我不需要创建一个实现 IAppSettings 的类 - 适配器会动态执行此操作。另外,虽然在本例中我只是阅读,但理论上如果我在 appSettings 上设置属性值,适配器将使底层字典与这些更改保持同步。
I'd highly recommend the Castle DictionaryAdapter, easily one of that project's best-kept secrets. You only need to define an interface with the properties you want, and in one line of code the adapter will generate an implementation, instantiate it, and synchronize its values with a dictionary you pass in. I use it to strongly-type my AppSettings in a web project:
Note that I did not need to create a class that implements IAppSettings - the adapter does that on the fly. Also, although in this case I'm only reading, in theory if I were setting property values on appSettings, the adapter would keep the underlying dictionary in sync with those changes.
我认为你应该使用反射。像这样的事情:
它获取你的字典并循环它并设置值。你应该让它变得更好,但这只是一个开始。你应该这样称呼它:
I think you should use reflection. Something like this:
It takes your dictionary and loops through it and sets the values. You should make it better but it's a start. You should call it like this:
反射可以通过迭代属性将您从对象带到字典。
要采用另一种方式,您必须使用动态 ExpandoObject (事实上,它已经继承自 IDictionary,因此已经为您完成了此操作)在 C# 中,除非您可以以某种方式从字典中的条目集合推断出类型。
因此,如果您使用 .NET 4.0,请使用 ExpandoObject,否则您有很多工作要做...
Reflection can take you from an object to a dictionary by iterating over the properties.
To go the other way, you'll have to use a dynamic ExpandoObject (which, in fact, already inherits from IDictionary, and so has done this for you) in C#, unless you can infer the type from the collection of entries in the dictionary somehow.
So, if you're in .NET 4.0 land, use an ExpandoObject, otherwise you've got a lot of work to do...
基于 Matías Fidemraizer 的答案,这里是一个支持绑定到字符串以外的对象属性的版本。
Building on Matías Fidemraizer's answer, here is a version that supports binding to object properties other than strings.
如果您使用的是 Asp.Net MVC,那么请看一下:
这是 System.Web.Mvc.HtmlHelper 类上的静态公共方法。
If you are using Asp.Net MVC, then take a look at:
which is a static public method on the System.Web.Mvc.HtmlHelper class.