How to access private members of a base class

G

Guest

Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance
 
M

Mike Hofer

Abelardo said:
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance

Theoretically, there shouldn't be. That's why they're private. But I
wouldn't be surprised if .NET provided some way to do it. I'd be
interested in finding out.
 
N

Nicholas Paldino [.NET/C# MVP]

Abelardo,

You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and then
calling Invoke on them (using the instance of the object).

If you have access to the code of the class, you might want to consider
exposing the methods as public/internal/protected (depending on where the
method calls are taking place).

Hope this helps.
 
G

Guest

Thank you,
Perhaps i should have been more explicit in my question.

I already use reflection, and it works half-way: Without
ReflectionPermission you cannot access the non-public members of a class (
that's why I wrote I had that permission out of the way)
Now, WITH ReflectionPermission, I can access ALL members, but ONLY on the
current class level. But I CANNOT obtain the PRIVATE members of the base
classes. (That is normal, because private members of a base class are NOT
members of the derived classes) - PROTECTED members are available, even if
they are declared on base classes -

I would like to know if there is a way around this.

Thank you

Nicholas Paldino said:
Abelardo,

You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and then
calling Invoke on them (using the instance of the object).

If you have access to the code of the class, you might want to consider
exposing the methods as public/internal/protected (depending on where the
method calls are taking place).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Abelardo Vacca said:
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the
private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance
 
N

Nicholas Paldino [.NET/C# MVP]

Abelardo,

If you have the type of the class, you can use the BaseType property to
get the base class. You can then use reflection on that type to get the
private members, and so on, and so on...


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Abelardo Vacca said:
Thank you,
Perhaps i should have been more explicit in my question.

I already use reflection, and it works half-way: Without
ReflectionPermission you cannot access the non-public members of a class (
that's why I wrote I had that permission out of the way)
Now, WITH ReflectionPermission, I can access ALL members, but ONLY on the
current class level. But I CANNOT obtain the PRIVATE members of the base
classes. (That is normal, because private members of a base class are NOT
members of the derived classes) - PROTECTED members are available, even if
they are declared on base classes -

I would like to know if there is a way around this.

Thank you

Nicholas Paldino said:
Abelardo,

You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and
then
calling Invoke on them (using the instance of the object).

If you have access to the code of the class, you might want to
consider
exposing the methods as public/internal/protected (depending on where the
method calls are taking place).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
Hi,
The title sums up the question pretty much.
I would like to access all private members of a class including the
private
members of its base classes.( I already have the ReflectionPermission )

Is there a way to get this information ?

Thnaks in advance
 
A

alantolan

Reflection allows you to access all aspects of a type directly. To set
a private field _val of the base class one can use

Type t = this.GetType().BaseType;

t.InvokeMember("_val",
BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.SetField,
null,
this,
new Object[] {23} );

The above will set an integer private member of the base class ( _val)
to 23

hth,
Alan.
 
M

Mike Hofer

Nicholas said:
You will only be able to access this information through reflection,
getting the MethodInfo instances representing the private methods, and then
calling Invoke on them (using the instance of the object).

WOW. Is it just me, or does that look like a big, huge, gaping security
loophole?

If I write a class and mark the members as private, they should remain
private, and no one should be able to see that data. That's the whole
POINT of private data. But this seems to indicate that someone can
instantiate my class, inspect it with reflection, and then change my
private data, thereby invalidating my object state.

Someone please, please, PLEASE tell me this isn't true.
 
T

The Crow

using private members is not designed as a security mechanism. it tells just
they "shouldnt" be used by consumers of the class. instead, you should
inspect code access security i think. and reflection mechanism is available
in java too.
 
M

Mike Hofer

The said:
using private members is not designed as a security mechanism. it tells just
they "shouldnt" be used by consumers of the class. instead, you should
inspect code access security i think. and reflection mechanism is available
in java too.

I apologize; I wasn't being clear. By "security" I was referring to the
integrity of an object's state, not security as in user names,
passwords, and encryption.

I'm concerned that a basic tenet of object-oriented design (private
data is not visible to anyone but the class that defines it) is untrue.

Here's my problem: when I hide a variable by making it private, I'm
doing that because I don't want anyone to access that variable, or to
muck with its value. In addition, I may change its type at some point
in the future. Hiding the variable is supposed to place a layer of
abstraction between the user and variable itself, so that I can change
the implementation as needed.

Anyway, back to my BIG concern. A private member should not be modified
or invoked by ANYONE outside the class that defines it. Doing so may
invalidate my object's state. That means that my object will fail to
meet its contractual obligation.

It just looks to me like Reflection breaks a rule of abstraction by
ignoring the definition of a private member.
 
J

Jon Skeet [C# MVP]

Mike Hofer said:
WOW. Is it just me, or does that look like a big, huge, gaping security
loophole?

If I write a class and mark the members as private, they should remain
private, and no one should be able to see that data. That's the whole
POINT of private data. But this seems to indicate that someone can
instantiate my class, inspect it with reflection, and then change my
private data, thereby invalidating my object state.

Someone please, please, PLEASE tell me this isn't true.

You can only access them if you're running with full trust. At that
stage, you almost certainly have enough access to the computer to use
other tools to get at any memory location anyway, and even if not, you
could modify the assembly to make the member public.

So in short, no, it doesn't add any security problems which aren't
already present anyway. If people can run your code with full trust,
you shouldn't have any information present which they shouldn't be able
to access (given enough time and energy).
 
J

Jon Skeet [C# MVP]

It just looks to me like Reflection breaks a rule of abstraction by
ignoring the definition of a private member.

It only breaks it if you take the special means necessary to break it -
i.e. using reflection.

Now, if you have enough permission to use reflection, you almost
certainly have enough access to decompile it, change the members to be
public etc.

Yes, people can do bad things like this - but only to code they'd
already be able to do bad things to in other ways.

Reflection *doesn't* let you access private members when you don't have
appropriate permission (i.e. when you're running in a lower trust
scenario).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top