使用Reflection获取PropertyInfo,只想查看有mutators的访问器
使用反射,我只想检索同时具有 get
和 set
方法的属性,并忽略仅具有 get
的属性。我想做的是向用户提供他/她能够更改的变量列表,因此向他们显示仅具有 get
方法的属性会产生误导。
鉴于下面的代码,将仅向用户显示Name
。或者我可以向他们展示两者,但将 UniqueID
显示为灰色,这样他们就知道无法更改它。
public Int64 UniqueID
{
get { return this.uniqueID; }
}
public String Name
{
get { return this.name; }
set { this.name = value; }
}
背景信息:我正在使用 C# 4.0。
Using reflection I want to retrieve only the properties that have both a get
and a set
method, and ignore ones with only a get
. What I'm trying to do is give the user a list of variables that he/she is able to change, so showing them properties that only have a get
method is misleading.
Given this code below, the user would only be shown Name
. Or I could possibly show them both, but grey-out UniqueID
so they know they cannot change it.
public Int64 UniqueID
{
get { return this.uniqueID; }
}
public String Name
{
get { return this.name; }
set { this.name = value; }
}
Background Info: I'm using C# 4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
CanRead
和CanWrite
属性:请注意,上述查询仅查找具有公共访问器的公共属性。
You can use the
CanRead
andCanWrite
properties:Do note that the above query looks only for public properties with public accessors.
我认为您正在寻找的属性是
PropertyInfo.CanWrite
,这可以按以下方式实现,以检查 Get 和 Set ,如下所示:I think the property you are looking for is
PropertyInfo.CanWrite
and this can be implemented as the following to check both Get and Set with something like: