使用反射设置对象属性
C# 中有没有一种方法可以使用反射来设置对象属性?
例如:
MyObject obj = new MyObject();
obj.Name = "Value";
我想用反射设置 obj.Name 。比如:
Reflection.SetProperty(obj, "Name") = "Value";
有办法做到这一点吗?
Is there a way in C# where I can use reflection to set an object property?
Ex:
MyObject obj = new MyObject();
obj.Name = "Value";
I want to set obj.Name
with reflection. Something like:
Reflection.SetProperty(obj, "Name") = "Value";
Is there a way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,您可以使用
Type.InvokeMember()
:如果
obj
没有名为Name
的属性,这将引发异常,或者它无法设置。另一种方法是获取属性的元数据,然后设置它。这将允许您检查该属性是否存在,并验证它是否可以设置:
Yes, you can use
Type.InvokeMember()
:This will throw an exception if
obj
doesn't have a property calledName
, or it can't be set.Another approach is to get the metadata for the property, and then set it. This will allow you to check for the existence of the property, and verify that it can be set:
您还可以执行以下操作:
其中 target 是将设置其属性的对象。
You can also do:
where target is the object that will have its property set.
反射,基本上,即
或者有库可以在便利性和性能方面提供帮助;例如 FastMember:
(它的优点是不需要提前知道它是否是一个字段与财产)
Reflection, basically, i.e.
or there are libraries to help both in terms of convenience and performance; for example with FastMember:
(which also has the advantage of not needing to know in advance whether it is a field vs a property)
或者,您可以将 Marc 的一个衬垫包装在您自己的扩展类中:
并像这样调用它:
为了更好地衡量,让我们添加一个方法来获取属性值:
Or you could wrap Marc's one liner inside your own extension class:
and call it like this:
For good measure, let's add a method to get a property value:
使用这样的东西:
或
Use somethings like this :
or
您还可以使用类似的方式访问字段:
通过反射,一切都可以是一本打开的书:) 在我的示例中,我们绑定到私有实例级字段。
You can also access fields using a simillar manner:
With reflection everything can be an open book:) In my example we are binding to a private instance level field.
当您想要使用属性名称从另一个对象批量分配一个对象的属性时,您可以尝试此操作:
You can try this out when you want to mass-assign properties of an Object from another Object using Property names:
我刚刚发布了一个 Nuget 包,它不仅允许设置第一级属性,还允许设置给定对象中任意深度的嵌套属性。
这是包
通过对象从根开始的路径设置对象属性的值。
对象可以是复杂对象,属性可以是多级深层嵌套属性,也可以是直接在根下的属性。
ObjectWriter
将使用属性路径参数查找属性并更新其值。属性路径是从根节点到我们要设置的结束节点属性访问的属性的附加名称,由分隔符字符串参数分隔。用法:
用于直接在对象根下设置属性:
即。
LineItem
类有一个名为ItemId
的 int 属性,用于在对象根以下设置多个级别的嵌套属性:
即。
Invite
类有一个名为State
的属性,该属性有一个名为Invite
的属性(Invite 类型),该属性有一个名为Recipient 的属性
,它有一个名为Id
的属性。让事情变得更复杂的是,
State
属性不是引用类型,它是一个struct
。以下是如何在单行中设置对象树底部的 Id 属性(“outlook”的字符串值)。
I have just published a Nuget package that allows setting up not only the first level Properties but also nested properties in the given object in any depth.
Here is the package
Sets the value of a property of an object by its path from the root.
The object can be a complex object and the property can be multi level deep nested property or it can be a property directly under the root.
ObjectWriter
will find the property using the property path parameter and update its value. Property path is the appended names of the properties visited from root to the end node property which we want to set, delimited by the delimiter string parameter.Usage:
For setting up the properties directly under the object root:
Ie.
LineItem
class has an int property calledItemId
For setting up nested property multiple levels below the object root:
Ie.
Invite
class has a property calledState
, which has a property calledInvite
(of Invite type), which has a property calledRecipient
, which has a property calledId
.To make things even more complex, the
State
property is not a reference type, it is astruct
.Here is how you can set the Id property (to string value of “outlook”) at the bottom of the object tree in a single line.
根据MarcGravell的建议,我构建了以下静态方法。该方法一般使用FastMember将所有匹配属性从源对象分配给目标
Based on MarcGravell's suggestion, I have constructed the following static method.The method generically assigns all matching properties from source object to target using FastMember