使用反射获取基类的受保护属性值 [英] Get protected property value of base class using reflection

查看:98
本文介绍了使用反射获取基类的受保护属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以访问位于该对象的基类中并且是私有的ConfigurationId属性的值.我试图倒霉没有运气.

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. I have tried to do it with reflection with no luck.

推荐答案

要访问ConfigurationId属性,我使用了以下代码:

To access ConfigurationId property i have used following code:

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);

这篇关于使用反射获取基类的受保护属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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