【发布时间】:2013-03-12 10:34:33
【问题描述】:
我想创建一个动态类,执行以下操作:
-
我有一个字典,其中键是整数,值是字符串。
Dictionary<int, string> PropertyNames = new Dictionary<int, string>(); PropertyNames.Add(2, "PropertyName1"); PropertyNames.Add(3, "PropertyName2"); PropertyNames.Add(5, "PropertyName3"); PropertyNames.Add(7, "PropertyName4"); PropertyNames.Add(11,"PropertyName5"); -
我想将此字典传递给类构造函数,该构造函数将属性构建到类实例中。并且假设我想为每个属性同时拥有获取和设置功能。例如:
MyDynamicClass Props = new MyDynamicClass( PropertyNames ); Console.WriteLine(Props.PropertyName1); Console.WriteLine(Props.PropertyName2); Console.WriteLine(Props.PropertyName3); Props.PropertyName4 = 13; Props.PropertyName5 = new byte[17];
我无法理解DLR。
【问题讨论】:
-
看到这篇文章,我想这会有所帮助:stackoverflow.com/questions/2974008/…
-
您基本上是在描述
ExpandoObject。看看:msdn.microsoft.com/en-us/library/… -
哦,好吧,我认为这很简单。我从来不知道 ExpandoObject。
-
只是出于好奇,有谁知道为什么 MSFT 决定将类命名为 ExpandoObject 而不是 ExpandObject?这似乎是一个错字。
-
仅供参考,当您使用 ExpandoObject 或 DynamicObject 等类型时,您将放弃几乎所有编译时检查并牺牲性能。
标签: c# .net dynamic properties