473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Getting a private variable through Reflection?

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
Aug 17 '06 #1
11 25367
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...
>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

Aug 17 '06 #2

"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.
You use GetField().

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

-- Alan
Aug 17 '06 #3
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.

--
David Veeneman
Foresight Systems
Aug 17 '06 #4
"David Veeneman" <da****@nospam.com (domain is my last name)wrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
object fieldData = fieldInfo.GetValue(components);
Try

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

-- Alan
Aug 17 '06 #5
That did it! Thanks agsin.

--
David Veeneman
Foresight Systems
Aug 17 '06 #6
This is pretty cool, I learned something new.

"Alan Pretre" <no@spamwrote in message
news:er**************@TK2MSFTNGP06.phx.gbl...
"David Veeneman" <da****@nospam.com (domain is my last name)wrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
>object fieldData = fieldInfo.GetValue(components);

Try

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

-- Alan


Aug 17 '06 #7
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
Foresight Systems
Aug 17 '06 #8
>
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.

--
David Veeneman
Foresight Systems
Aug 17 '06 #9


"David Veeneman" <da****@nospam.com (domain is my last name)wrote in
message news:OC**************@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
Aug 17 '06 #10
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?

--
David Veeneman
Foresight Systems
Aug 17 '06 #11
"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/articl...&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/conten...ks/09fig05.gif
--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Aug 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SteveS | last post by:
What's the difference in using the private variable as opposed to the actual property in a class module? Which is better or more efficient? ( LoginObject.Save (_loginName) or LoginObject.Save...
3
by: jeko | last post by:
Hi, I'm using a class method which read and write on private variable of own class. During the first alghoritm cycle, method read and write correclty on num_elts variable, but during the second...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
3
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however,...
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
6
by: felicia | last post by:
Hi, I am having problem to retrieve the data value of a public variable. Below is my code: Form1 Option Explicit Public selected_entry_id As String Private Sub btnEdit_Click() ...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
3
by: seberino | last post by:
Bad form to access a *private variable* like _foo? The reason I'm asking is that TurboGears/SQLObject mobel objects have an attribute called "_connection" that must be used to manually commit...
3
Plater
by: Plater | last post by:
I am having some trouble finding a way to retreive the operators from a custom class. In a simplified example I have the following: public class Namespace1.Address { public string City;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.