反映从基类的私人领域 [英] Reflecting a private field from a base class

查看:130
本文介绍了反映从基类的私人领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是结构:

MyClass的:SuperClass2

SuperClass2:SuperClass1

superClass2是Product.Web和SuperClass1是.NET System.Web程序集

我试图迫使一个值上SuperClass1私人布尔领域。但是,不管我怎么努力我不能下地回来的反射。

我用下面的code不同BindingFlag组合,但没有迄今工作。 SuperClass1是一个抽象类。

 ((SuperClass1)本).GetType()GetFields(System.Reflection.BindingFlags.NonPublic)。
 

注: 当我使用的GetProperties,()我得到一个漂亮的大名单,但是当我指定任何的BindingFlags我什么也没有,即使有匹配的属性。请告诉我交易?

此外,现场没有标记内部的

Obvisouly我将使用getfield命令(字符串名称,的BindingFlags),但我甚至无法获得GetFlags()工作。

更新:我已经尝试添加BindingFlags.Instance的建议,但它不工作(如预期反正)。我回来2场被从类SuperClass1从继承的到来。与getfield命令(字符串名称,旗)

使用时返回null

下面是code基类,我试图让该领域

 公共抽象类BaseValidator:标签,IValidator
  {
    私人布尔propertiesChecked;
...
}
 

解决方案

您可以手动去在继承链来获取基本领域:

考虑到这些类:

 类SuperClass1
{
    私人诠释MyField的;
}

类SuperClass2:SuperClass1
{
}

MyClass类:SuperClass2
{

}
 

这应该工作:

  VAR MyObj中=新MyClass的();
VAR MyField的= typeof运算(MyClass的).BaseType
                             .BaseType
                             .GetField(MyField的,BindingFlags.Instance | BindingFlags.NonPublic可);
 

有没有在这这样,请回答一个更通用的解决方案: C#中的GetType()GetFields问题

Here is the structure:

MyClass : SuperClass2

SuperClass2 : SuperClass1

superClass2 is in Product.Web and SuperClass1 is in the .NET System.Web assembly

I'm trying to force a value into a private bool field on SuperClass1. But not matter what I try I cannot get the fields to come back from reflection.

I'm using the following code with different BindingFlag combinations but nothing has worked so far. SuperClass1 is an abstract class.

((SuperClass1)this).GetType().GetFields(System.Reflection.BindingFlags.NonPublic);

Notes: When I use GetProperties() I get back a nice big list but when I specify any bindingflags I get nothing even though there are matching properties. Whats the deal?

Also, the field is not marked internal

Obvisouly I would be using GetField(string name, BindingFlags) but I can't even get GetFlags() to work.

Update: I've tried adding BindingFlags.Instance as suggested but it doesn't work (as expected anyway). I get back 2 fields that are coming from the class SuperClass1 inherits from. Returns null when used with GetField(string name, Flags)

Here is the code for the base class I'm trying to get the field for

public abstract class BaseValidator : Label, IValidator
  {
    private bool propertiesChecked;
...
}

解决方案

You can manually go up in the inheritance chain to get the base fields:

Given these classes:

class SuperClass1
{
    private int myField;
}

class SuperClass2 : SuperClass1
{
}

class MyClass : SuperClass2
{

}

This should work:

var myObj = new MyClass();
var myField = typeof(MyClass).BaseType
                             .BaseType
                             .GetField("myField", BindingFlags.Instance | BindingFlags.NonPublic);

There's a more generic solution in this SO answer: C# GetType().GetFields problem

这篇关于反映从基类的私人领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆