如何动态创建类
我需要动态创建一个类对象。我尝试使用动态关键字。
dynamic dataTransferObject = new dtoClass();
dataTransferObject.Property1= "someValue";
dataTransferObject.Property2= "someOtherValue";
LogicLayer.Update(dataTransferObject);
我将解释该对象以在逻辑层内部执行进一步的操作。编译器不喜欢我的语法,请指教!
I need to create a class object dynamically. I attempted this using the dynamic keyword.
dynamic dataTransferObject = new dtoClass();
dataTransferObject.Property1= "someValue";
dataTransferObject.Property2= "someOtherValue";
LogicLayer.Update(dataTransferObject);
I will interpret the object to perform further action inside of the logic layer. The compiler does not like my syntax, please advise!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ExpandoObject 来完成此操作。
use the ExpandoObject to accomplish this.
我想这可能就是您正在寻找的!
http://www.hanselman.com/blog/NuGetPackageOfTheWeek6DynamicMalleableEnjoyableExpandoObjectsWithClay.aspx
转至该部分称为“Expandos 和 Dynamic” - 它允许您需要执行以下操作:
Stu
I think this might be what you're looking for!
http://www.hanselman.com/blog/NuGetPackageOfTheWeek6DynamicMalleableEnjoyableExpandoObjectsWithClay.aspx
Go to the section called "Expandos and Dynamic" - it allows you to do the following:
Stu
尝试使用匿名类型。检查以下代码:
匿名类型提供了一种将一组只读属性封装到单个对象中的便捷方法,而无需首先显式定义类型。
try to use anonymous type. check following code:
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.