如何动态创建类

发布于 2024-11-18 20:26:57 字数 328 浏览 1 评论 0原文

我需要动态创建一个类对象。我尝试使用动态关键字。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野心澎湃 2024-11-25 20:26:57

使用 ExpandoObject 来完成此操作。

dynamic dataTransferObject = new System.Dynamic.ExpandoObject();
dataTransferObject.Property1 = "someValue";
dataTransferObject.Property2 = "someOtherValue";

use the ExpandoObject to accomplish this.

dynamic dataTransferObject = new System.Dynamic.ExpandoObject();
dataTransferObject.Property1 = "someValue";
dataTransferObject.Property2 = "someOtherValue";
赠我空喜 2024-11-25 20:26:57

我想这可能就是您正在寻找的!

http://www.hanselman.com/blog/NuGetPackageOfTheWeek6DynamicMalleableEnjoyableExpandoObjectsWithClay.aspx

转至该部分称为“Expandos 和 Dynamic” - 它允许您需要执行以下操作:

var person = New.Person();
person.FirstName = "Louis";
person.LastName = "Dejardin";

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:

var person = New.Person();
person.FirstName = "Louis";
person.LastName = "Dejardin";

Stu

半城柳色半声笛 2024-11-25 20:26:57

尝试使用匿名类型。检查以下代码:

var v = new { Property1 = "someValue", Property2 = "someOtherValue" };

匿名类型提供了一种将一组只读属性封装到单个对象中的便捷方法,而无需首先显式定义类型。

try to use anonymous type. check following code:

var v = new { Property1 = "someValue", Property2 = "someOtherValue" };

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文