Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting a private variable through Reflection?

397 views
Skip to first unread message

David Veeneman

unread,
Aug 17, 2006, 10:16:27 AM8/17/06
to
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


Marina Levit [MVP]

unread,
Aug 17, 2006, 10:21:54 AM8/17/06
to
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" <dav...@nospam.com (domain is my last name)> wrote in
message news:%23fRhfeg...@TK2MSFTNGP03.phx.gbl...

Alan Pretre

unread,
Aug 17, 2006, 10:31:11 AM8/17/06
to

"David Veeneman" <dav...@nospam.com (domain is my last name)> wrote in
message news:%23fRhfeg...@TK2MSFTNGP03.phx.gbl...
>I need to get a parent's private variable from within a control.

You use GetField().

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetfieldtopic.asp

-- Alan


David Veeneman

unread,
Aug 17, 2006, 11:36:50 AM8/17/06
to
get a parent's private variable from within a control.
>
> You use GetField().
>

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.

Alan Pretre

unread,
Aug 17, 2006, 11:51:18 AM8/17/06
to
"David Veeneman" <dav...@nospam.com (domain is my last name)> wrote in
message news:%23Z$hxLhwG...@TK2MSFTNGP06.phx.gbl...
> object fieldData = fieldInfo.GetValue(components);

Try

object fieldData = fieldInfo.GetValue(this.Parent);

-- Alan


David Veeneman

unread,
Aug 17, 2006, 1:22:36 PM8/17/06
to
That did it! Thanks agsin.

Marina Levit [MVP]

unread,
Aug 17, 2006, 1:27:33 PM8/17/06
to
This is pretty cool, I learned something new.

"Alan Pretre" <no@spam> wrote in message
news:erho7Thw...@TK2MSFTNGP06.phx.gbl...

David Veeneman

unread,
Aug 17, 2006, 1:31:26 PM8/17/06
to
Here's the code for a control to iterate through the System.ComponentModel
components of its parent:

// Get the parent's components collection


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

IContainer parent = (IContainer)fieldInfo.GetValue(this.Parent);

// Check for a helper component
foreach (Component component in parent.Components)
{
if (component.GetType() == typeof(MyHelperComponent))
{
m_Helper = (MyHelperComponent)component;
}
}

Thanks again to Alan Pretre for his help.

David Veeneman

unread,
Aug 17, 2006, 1:32:37 PM8/17/06
to
>
> I don't know of a way to get the private variable using reflection.
>

Actually, it can be done! See later messages in this thread.

Alan Pretre

unread,
Aug 17, 2006, 3:02:38 PM8/17/06
to

"David Veeneman" <dav...@nospam.com (domain is my last name)> wrote in

message news:OCjE0Liw...@TK2MSFTNGP06.phx.gbl...


> Here's the code for a control to iterate through the System.ComponentModel
> components of its parent:

Note that in general it is unsafe to access private members of a class due
to the fact that future changes might be made to the thing you're prying
into. I got bit by this once with the switch from the .NET framework 1.x to
2.0. Was prying into something, which then disappeared in the later
release.

-- Alan


David Veeneman

unread,
Aug 17, 2006, 5:57:21 PM8/17/06
to
> Note that in general it is unsafe to access private members of a class due
> to the fact that future changes might be made to the thing you're prying
> into.

Thanks--point well taken. I don't have a lot of experience with reflection
because I avoid it like the plague, for the very reasons you point out.

Private variables are private for a reason, and one normally ought not go
poking around where not invited. In this case, it was literally the only way
to do what needed to be done.

What I can't figure out is, what's the rationale behind putting the
components collection in a private Designer variable, when the controls
collection is a property of the Form class?

Mini-Tools Timm

unread,
Aug 18, 2006, 8:50:03 AM8/18/06
to
"David Veeneman" wrote:

> 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.

If you are trying to read private members via Reflection, that's a good sign
you should probably look for another solution. ;-) Breaking encapsulation
in an object-oriented language like C# is just asking for trouble.

Luckily, .NET made it easy to access all components on a form through public
properties. Following is an excellent article that explains how design-time
development works:

http://www.awprofessional.com/articles/article.asp?p=169528&seqNum=2&rl=1

Specifically, here is a diagram that shows the relationship between a
control/form and all the controls and components on that form:

http://www.awprofessional.com/content/images/chap9_0321116208/elementLinks/09fig05.gif


--
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com

0 new messages