找到一个带有 Reflection 的私有字段?
给定这个类,
class Foo
{
// Want to find _bar with reflection
[SomeAttribute]
private string _bar;
public string BigBar
{
get { return this._bar; }
}
}
我想找到私有项目 _bar,我将用属性标记它。 那可能吗?
我已经对属性进行了此操作,我在其中查找了属性,但从未查找私有成员字段。
我需要设置哪些绑定标志来获取私有字段?
Given this class
class Foo
{
// Want to find _bar with reflection
[SomeAttribute]
private string _bar;
public string BigBar
{
get { return this._bar; }
}
}
I want to find the private item _bar that I will mark with a attribute. Is that possible?
I have done this with properties where I have looked for an attribute, but never a private member field.
What are the binding flags that I need to set to get the private fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用
BindingFlags.NonPublic
和BindingFlags.Instance
标志Use
BindingFlags.NonPublic
andBindingFlags.Instance
flags您可以像使用属性一样执行此操作:
You can do it just like with a property:
使用反射获取私有变量的值:
使用反射设置私有变量的值:
其中 objectForFooClass 是类类型 Foo 的非 null 实例。
Get private variable's value using Reflection:
Set value for private variable using Reflection:
Where objectForFooClass is a non null instance for the class type Foo.
具有扩展方法的良好语法
您可以使用如下代码访问任意类型的任何私有字段:
为此,您需要定义一个扩展方法来为您完成工作:
Nice Syntax With Extension Method
You can access any private field of an arbitrary type with code like this:
For that you need to define an extension method that will do the work for you:
在反映私有成员时需要注意的一件事是,如果您的应用程序在中等信任度下运行(例如,当您在共享托管环境上运行时),它不会找到它们 - BindingFlags.NonPublic 选项将被忽略。
One thing that you need to be aware of when reflecting on private members is that if your application is running in medium trust (as, for instance, when you are running on a shared hosting environment), it won't find them -- the BindingFlags.NonPublic option will simply be ignored.
下面是一些简单的获取和设置私有字段和属性(带有 setter 的属性)的扩展方法:
使用示例:
代码:
Here is some extension methods for simple get and set private fields and properties (properties with setter):
usage example:
Code:
我个人使用这个方法
I use this method personally
是的,但是您需要设置绑定标志来搜索私有字段(如果您正在寻找类实例之外的成员)。
您需要的绑定标志是:System.Reflection.BindingFlags.NonPublic
Yes, however you will need to set your Binding flags to search for private fields (if your looking for the member outside of the class instance).
The binding flag you will need is: System.Reflection.BindingFlags.NonPublic
如果您的.Net框架大于4.5。 您可以使用 GetRuntimeFields 方法。
此方法返回在指定类型上定义的所有字段,包括继承字段、非公共字段、实例字段和静态字段。
https://learn. microsoft.com/en-us/dotnet/api/system.reflection.runtimereflectionextensions.getruntimefields?view=net-6.0
If your .Net framework is greater than 4.5. You can use GetRuntimeFields method.
This method returns all fields that are defined on the specified type, including inherited, non-public, instance, and static fields.
https://learn.microsoft.com/en-us/dotnet/api/system.reflection.runtimereflectionextensions.getruntimefields?view=net-6.0
我在谷歌搜索时发现了这个,所以我意识到我正在碰触旧帖子。 但是 GetCustomAttributes 需要两个参数。
第二个参数指定是否要搜索继承层次结构
I came across this while searching for this on google so I realise I'm bumping an old post. However the GetCustomAttributes requires two params.
The second parameter specifies whether or not you wish to search the inheritance hierarchy