473,387 Members | 3,684 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,387 software developers and data experts.

Relection and private object properties

Hi

I have the following scenario...

public class aclass
{
private bclass mbclass

public aclass()
{
bclass = new bclass();
}
}

.... there is more but I cut out the interesting bits. My question is
whether or not by reflection I can in someway modify the value of (for
instance) bclass.customername whilst reflecting on an object of type aclass.
I would prefer to do it as though I was sat in aclass i.e.
bclass.customername = "foo" sort of thing . The code I use at the moment
assumes all fields are within the reflected object (aclass), so if somehow I
could make bclass.customername accessable through the aclass reflection I
would be most happy.

Thanks in advance

Chris.
Nov 16 '05 #1
7 2725
Reflection should let you access any private or protected members unless are
specifically protected using some security policies.

"Tilted" <pu***@gvygrq.pb.hx (ROT13)> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
Hi

I have the following scenario...

public class aclass
{
private bclass mbclass

public aclass()
{
bclass = new bclass();
}
}

... there is more but I cut out the interesting bits. My question is
whether or not by reflection I can in someway modify the value of (for
instance) bclass.customername whilst reflecting on an object of type aclass. I would prefer to do it as though I was sat in aclass i.e.
bclass.customername = "foo" sort of thing . The code I use at the moment
assumes all fields are within the reflected object (aclass), so if somehow I could make bclass.customername accessable through the aclass reflection I
would be most happy.

Thanks in advance

Chris.

Nov 16 '05 #2
Thanks for answering Peter

So if I am reflecting aclass which has a member of type bclass, I should be
able to access a member of bclass from aclass through a bclass.customername
type of syntax. Obviously bclass.customername is not actually a member of
aclass yet an object of type bclass is.

If I can do this any chance of a pointer in the right direction as to how I
should code it up.

Cheers
Chris.
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
Reflection should let you access any private or protected members unless
are
specifically protected using some security policies.

"Tilted" <pu***@gvygrq.pb.hx (ROT13)> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
Hi

I have the following scenario...

public class aclass
{
private bclass mbclass

public aclass()
{
bclass = new bclass();
}
}

... there is more but I cut out the interesting bits. My question is
whether or not by reflection I can in someway modify the value of (for
instance) bclass.customername whilst reflecting on an object of type

aclass.
I would prefer to do it as though I was sat in aclass i.e.
bclass.customername = "foo" sort of thing . The code I use at the moment
assumes all fields are within the reflected object (aclass), so if
somehow

I
could make bclass.customername accessable through the aclass reflection I
would be most happy.

Thanks in advance

Chris.


Nov 16 '05 #3
Tilted <pu***@gvygrq.pb.hx> wrote:
So if I am reflecting aclass which has a member of type bclass, I should be
able to access a member of bclass from aclass through a bclass.customername
type of syntax. Obviously bclass.customername is not actually a member of
aclass yet an object of type bclass is.

If I can do this any chance of a pointer in the right direction as to how I
should code it up.


You go through it in steps - first get bclass, then get the type of
that and then get the customername property or field from that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanks

Thats what I didn't want to hear though, I knew that would be the answer as
it makes sense yet slows my app down. No other way then??? Probably not,
looks like I'm going to have to take hit in the performance of the app.

Thanks anyway, glad to see the groups are still nicely responsive, been a
while since I've had to venture down here but well worth the visit.

Cheers

Chris.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tilted <pu***@gvygrq.pb.hx> wrote:
So if I am reflecting aclass which has a member of type bclass, I should
be
able to access a member of bclass from aclass through a
bclass.customername
type of syntax. Obviously bclass.customername is not actually a member
of
aclass yet an object of type bclass is.

If I can do this any chance of a pointer in the right direction as to how
I
should code it up.


You go through it in steps - first get bclass, then get the type of
that and then get the customername property or field from that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Tilted <pu***@gvygrq.pb.hx> wrote:
Thats what I didn't want to hear though, I knew that would be the answer as
it makes sense yet slows my app down. No other way then??? Probably not,
looks like I'm going to have to take hit in the performance of the app.
Reflection is *always* going to add a performance hit. If you're going
to be going down the same paths repeatedly, you can improve things by
caching the FieldInfo or PropertyInfo of each part, so you only have to
get the values rather than finding them each time.
Thanks anyway, glad to see the groups are still nicely responsive, been a
while since I've had to venture down here but well worth the visit.


Goodo :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Something like this (the type and variable names have been changed to protect the innocent)

using System;
using System.Reflection;

class App
{
static void Main(string[] args)
{
Foo f = new Foo();
ChangeQuux(f);
f.Report();
}

static void ChangeQuux(object o)
{
Type fooType = o.GetType();
FieldInfo barField = fooType.GetField("b", BindingFlags.Instance | BindingFlags.NonPublic);
object bar = barField.GetValue(o);
Type barType = bar.GetType();
FieldInfo quuxField = barType.GetField("quux", BindingFlags.Instance | BindingFlags.NonPublic);
quuxField.SetValue(bar, "TADA!!");
}

class Foo
{
Bar b = new Bar();
public void Report()
{
Console.WriteLine(b.Quux);
}
}

class Bar
{
string quux = "Hmm";
public string Quux
{
get{ return quux; }
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Tilted <pu***@gvygrq.pb.hx> wrote:
So if I am reflecting aclass which has a member of type bclass, I should be
able to access a member of bclass from aclass through a bclass.customername
type of syntax. Obviously bclass.customername is not actually a member of
aclass yet an object of type bclass is.

If I can do this any chance of a pointer in the right direction as to how I
should code it up.


You go through it in steps - first get bclass, then get the type of
that and then get the customername property or field from that.
Nov 16 '05 #7
Thanks Richard

Pretty much knoew the "correct" way of doing it, just thought there might be
a short cut, obviously not.

Thanks anyway.

Chris.

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
Something like this (the type and variable names have been changed to
protect the innocent)

using System;
using System.Reflection;

class App
{
static void Main(string[] args)
{
Foo f = new Foo();
ChangeQuux(f);
f.Report();
}

static void ChangeQuux(object o)
{
Type fooType = o.GetType();
FieldInfo barField = fooType.GetField("b", BindingFlags.Instance |
BindingFlags.NonPublic);
object bar = barField.GetValue(o);
Type barType = bar.GetType();
FieldInfo quuxField = barType.GetField("quux", BindingFlags.Instance |
BindingFlags.NonPublic);
quuxField.SetValue(bar, "TADA!!");
}

class Foo
{
Bar b = new Bar();
public void Report()
{
Console.WriteLine(b.Quux);
}
}

class Bar
{
string quux = "Hmm";
public string Quux
{
get{ return quux; }
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Tilted <pu***@gvygrq.pb.hx> wrote:
So if I am reflecting aclass which has a member of type bclass, I should
be
able to access a member of bclass from aclass through a
bclass.customername
type of syntax. Obviously bclass.customername is not actually a member
of
aclass yet an object of type bclass is.

If I can do this any chance of a pointer in the right direction as to
how I
should code it up.


You go through it in steps - first get bclass, then get the type of
that and then get the customername property or field from that.

Nov 16 '05 #8

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

Similar topics

10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
0
by: Tilted | last post by:
Hi (sorry if this pops up twice, it appeared and then removed itself from my reader) I have the following scenario... public class aclass { private bclass mbclass
5
by: jg | last post by:
I followed the pattern in the MSDN dot net example for .net server and I tried a making simple .net dll as COM from Option Explicit On Option Strict On Imports System Imports...
6
by: David Whitchurch-Bennett | last post by:
Hi There, I have created a very simple web user control, containing only a combo box. I have created a class which contains some private object variables, for example... Private _anObject as...
0
by: John Wright | last post by:
How can I set the property of a loaded Assembly using reflection. My Shell program will log in a person and retrieve a list of all programs the person can use. When the shell program launches a...
4
by: jupitermoonbeam | last post by:
Hello, I wondered if anyone could help. I wish to retrieve the PropertyInfo of a specific interface but can't work out how to do this in a type safe way. Basically I don't want to do this:...
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
0
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Using a typed Dataset. From debugging I can see that each dataset.<tableis filled with the correct data (see ###) cmbxPayee DisplayMember, ValueMember and DataSource each show the expected...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.