简体   繁体   English

使用反射获取基类的受保护属性值

[英]Get protected property value of base class using reflection

I would like to know if it is possible to access the value of the ConfigurationId property which is located in the base class of the object and it's private.我想知道是否可以访问位于对象基类中的 ConfigurationId 属性的值,并且它是私有的。 I have tried to do it with reflection with no luck.我试图通过反思来做到这一点,但没有运气。 在此处输入图像描述

To access ConfigurationId property i have used following code:要访问 ConfigurationId 属性,我使用了以下代码:

SubsetController controller = new SubsetController(new CConfigRepository(new FakeDataContextRepository()));

var myBaseClassProtectedProperty=
            controller.GetType().BaseType
                .GetProperty("CCITenderInfo", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(controller);

var myProtectedProperty =
            CCITenderInfo.GetType()
                .GetProperty("ConfigurationId", BindingFlags.Public |     BindingFlags.Instance)
                .GetValue(myBaseClassProtectedProperty);

Assuming the following parent and child class:假设以下父类和子类:

class BaseClass
{
    private string privateField = "I'm Private";
}

class ChildClass : BaseClass
{

}

You can read privateField 's value from a ChildClass instance using reflection like this:您可以使用反射从ChildClass实例中读取privateField的值,如下所示:

ChildClass childInstance = new ChildClass();
object privateFieldValue = childInstance.GetType().BaseType
    .GetField("privateField", BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(childInstance);
Console.WriteLine(privateFieldValue); // I'm Private

To add to this answer - you should use the Instance and NonPublic binding flags for sure, but you should also ensure that you are actually referencing properties and not fields .要添加到这个答案 - 您应该肯定使用 Instance 和 NonPublic 绑定标志,但您还应该确保您实际上是在引用properties而不是fields

Eg if you have例如,如果你有

protected string Andrew;

You will not be able to get this via GetProperty , no matter what binding flags you use.无论您使用什么绑定标志,您都无法通过GetProperty获得此信息。 Why - because it is a field, and not a property...为什么 - 因为它是一个字段,而不是一个属性......

To fix this, just change it to要解决此问题,只需将其更改为

protected string Andrew {get;set;}

and then you can use the GetProperty method.然后您可以使用 GetProperty 方法。

Yes, this is possible with reflection.是的,这可以通过反射实现。

However, in order to look up nonpublic members, you'll need to use the reflection overload which take BindingFlags parameters.但是,为了查找非公共成员,您需要使用BindingFlags参数的反射重载。 In order to look up private members, you'll also need to access via the typeof the base class, even when using BindingFlags.FlattenHierarchy .为了查找private成员,您还需要通过基类的类型进行访问,即使使用typeof也是BindingFlags.FlattenHierarchy This also means you'll need to use the exact binding, however note that contradictory flags (such as using both NonPublic and Public ) are valid and will return either at that point.这也意味着您需要使用确切的绑定,但请注意,矛盾的标志(例如同时使用NonPublicPublic )是有效的,并且会在此时返回。

Be aware that the very need to look up nonpublic members could be considered code smell, and you should do so very carefully.请注意,查找非公共成员的非常需要可能被视为代码异味,您应该非常小心地这样做。 Also be aware that nonpublic members are not guaranteed to have the same names across different versions.另请注意,不保证非公共成员在不同版本中具有相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM