通过Reflection获取私有变量? [英] Getting a private variable through Reflection?

查看:88
本文介绍了通过Reflection获取私有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从控件中获取父级的私有变量。这就是

我正在做的事情:我有一个控件,我希望能够检测出特定的(b)特定(System.ComponentModel)组件的存在相同的形式。组件

不包含在Form.Controls集合中 - 它们包含在名为''components'的

私有Designer变量中。因此,要检测组件,

我将需要从控件的父级

表单中获取该私有变量。


有没有人有任何示例代码来从父母那里获取私有变量?

我是Reflection的新手,我正在努力解决它的问题。谢谢。


-

David Veeneman

远见系统

I need to get a parent''s private variable from within a control. Here''s what
I''m doing: I have a control that I want to be able to detect the presence of
a particular (System.ComponentModel) component on the same form. Components
aren''t contained in the Form.Controls collection--they are contained in a
private Designer variable called ''components''. So, to detect the component,
I''m going to need to get to that private variable from the control''s parent
form.

Does anyone have any sample code to get a private variable from a parent?
I''m new to Reflection, and I''m struggling a bit to sort it out. Thanks.

--
David Veeneman
Foresight Systems

推荐答案

是的,它将''components''声明为局部变量的方式是一个很大的痛苦。我不知道为什么会这样做 - 当你想要基类对组件做某事时,它会让继承成为一个巨大的痛苦。


我不知道如何使用反射来获取私有变量。


我必须做的是定义一个可覆盖的属性,然后是开发人员
继承类的
必须覆盖它,并返回''components''。那个

方式基类可以调用属性,并且多态,覆盖

版本将被调用,从而得到组件。


David Veeneman < da **** @ nospam.com(域名是我的姓)写在

消息新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...
Yes, the way it declares ''components'' as a local variable is a major pain. I
have no idea why this was done - it makes inheritance a big pain when you
want the base class to do something with the components.

I don''t know of a way to get the private variable using reflection.

What I had to do, was define an overridable property, and then the developer
of the inherited class has to override it, and return ''components''. That
way the base class can call the property, and polymorphically, the overriden
version will get called, thus getting the components.

"David Veeneman" <da****@nospam.com (domain is my last name)wrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...

>我需要从控件中获取父级的私有变量。这是我正在做的事情:我有一个控件,我希望能够在同一个
表单上检测特定(System.ComponentModel)组件的存在。组件不包含在Form.Controls集合中 - 它们包含在名为组件的私有Designer变量中。因此,要检测组件,我将需要从
控件的父窗体中获取该私有变量。


任何人都有任何示例代码从父母那里得到私人变量?

我是Reflection的新手,我正在努力解决它。谢谢。


-

David Veeneman

远见系统
>I need to get a parent''s private variable from within a control. Here''s
what I''m doing: I have a control that I want to be able to detect the
presence of a particular (System.ComponentModel) component on the same
form. Components aren''t contained in the Form.Controls collection--they are
contained in a private Designer variable called ''components''. So, to detect
the component, I''m going to need to get to that private variable from the
control''s parent form.

Does anyone have any sample code to get a private variable from a parent?
I''m new to Reflection, and I''m struggling a bit to sort it out. Thanks.

--
David Veeneman
Foresight Systems





" David Veeneman" < da **** @ nospam.com(域名是我的姓)写在

消息新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...

"David Veeneman" <da****@nospam.com (domain is my last name)wrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...

>我需要从控件中获取父级的私有变量。
>I need to get a parent''s private variable from within a control.



你使用GetField()。

http://msdn.microsoft.com/library/de...fieldtopic.asp


- Alan

You use GetField().

http://msdn.microsoft.com/library/de...fieldtopic.asp

-- Alan


从控件中获取父级的私有变量。
get a parent''s private variable from within a control.

>

你使用GetField()。
>
You use GetField().



谢谢, Alan--这就是我所拥有的。


类型parentForm = this.Parent.GetType();

FieldInfo fieldInfo = parentForm。 GetField(" components",

BindingFlags.Instance |

BindingFlags.NonPublic);

object fieldData = fieldInfo.GetValue(components);


GetField()有效,但GetValue()失败。这是我得到的错误:


类型''MyApp.FormMain1'上定义的字段''组件''不是
$ b $上的字段b目标对象,类型为''System.ComponentModel.Container''。


知道我错过了什么吗?再次感谢。


-

David Veeneman

远见系统

Thanks, Alan-- Here''s what I''ve got.

Type parentForm = this.Parent.GetType();
FieldInfo fieldInfo = parentForm.GetField("components",
BindingFlags.Instance |
BindingFlags.NonPublic);
object fieldData = fieldInfo.GetValue(components);

The GetField() works, but the GetValue() fails. Here''s the error I get:

Field ''components'' defined on type ''MyApp.FormMain1'' is not a field on the
target object which is of type ''System.ComponentModel.Container''.

Any idea what I''m missing? Thanks again.

--
David Veeneman
Foresight Systems


这篇关于通过Reflection获取私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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