473,614 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Compone ntModel) 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 25403
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******** ********@TK2MSF TNGP03.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.Compone ntModel) 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******** ********@TK2MSF TNGP03.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.Get Type();
FieldInfo fieldInfo = parentForm.GetF ield("component s",
BindingFlags.In stance |
BindingFlags.No nPublic);
object fieldData = fieldInfo.GetVa lue(components) ;

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

Field 'components' defined on type 'MyApp.FormMain 1' is not a field on the
target object which is of type 'System.Compone ntModel.Contain er'.

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******** ********@TK2MSF TNGP06.phx.gbl. ..
object fieldData = fieldInfo.GetVa lue(components) ;
Try

object fieldData = fieldInfo.GetVa lue(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******** ******@TK2MSFTN GP06.phx.gbl...
"David Veeneman" <da****@nospam. com (domain is my last name)wrote in
message news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>object fieldData = fieldInfo.GetVa lue(components) ;

Try

object fieldData = fieldInfo.GetVa lue(this.Parent );

-- Alan


Aug 17 '06 #7
Here's the code for a control to iterate through the System.Componen tModel
components of its parent:

// Get the parent's components collection
Type parentForm = this.Parent.Get Type();
FieldInfo fieldInfo = parentForm.GetF ield("component s",
BindingFlags.In stance |
BindingFlags.No nPublic);
IContainer parent = (IContainer)fie ldInfo.GetValue (this.Parent);

// Check for a helper component
foreach (Component component in parent.Componen ts)
{
if (component.GetT ype() == typeof(MyHelper Component))
{
m_Helper = (MyHelperCompon ent)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******** ******@TK2MSFTN GP06.phx.gbl...
Here's the code for a control to iterate through the System.Componen tModel
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

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

Similar topics

1
1686
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 (me.LoginName) FOR EXAMPLE: Class MyClass Private _loginName As String Public Property LoginName() As String
3
2150
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 cycle as soon as method wants read (or write) the variable program is aborted and return "segmentation fault". Why? num_elts is a long variable. thanks
6
4781
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 members are not returned. I did some digging and the MSDN documentation states that the caller must have ReflectionPermission in order to get the private members of a class. I am a little unfamiliar with this stipulation. I have checked the docs on...
3
2127
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, directly contradict what the VS 2003 help file says, and it seems that various posters to my original questions on Synclock disagree with each other. It seems, that the best practice is to use sysnlock to protect code, not variables. That...
2
8910
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 do That ? Multilinguage apps... If I'm able to do so, I already have an interface to enter the translation for each variables. Also, this way I don't need to deploy extra files for the translation.
6
1738
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() selected_entry_id = DataGrid1.Columns(0)
0
2034
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. Ok my problem is the following. I have a class that contains a "MakeByteArray" function. I have many objects of that class. Inside that function, I have a private variable, that is NOT static. It seems that when I put all these objects in...
3
938
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 database stuff.... e.g. MyObject._connection.commit() It bugs me. I don't know if it is a stylistic faux pas or not.
3
8473
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; public string State; public static implicit operator Address(Namespace2.Address value)
0
8198
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8591
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4138
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.