Relection and private object properties

  • Thread starter Thread starter Tilted
  • Start date Start date
T

Tilted

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.
 
Reflection should let you access any private or protected members unless are
specifically protected using some security policies.
 
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.
 
Tilted said:
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.
 
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.
 
Tilted said:
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 :)
 
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 said:
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.
 
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 said:
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 said:
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.
 
Back
Top