By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

How to access private members of a base class

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Abelardo Vacca wrote:
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.

Nov 17 '05 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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

Nov 17 '05 #3

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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


Nov 17 '05 #4

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Abelardo Vacca" <Ab***********@discussions.microsoft.com> wrote in
message
news:D4**********************************@microsof t.com...
> 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


Nov 17 '05 #5

P: n/a

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.

Nov 17 '05 #6

P: n/a

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Nov 17 '05 #7

P: n/a
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.
Nov 17 '05 #8

P: n/a

The Crow wrote:
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.

Nov 17 '05 #9

P: n/a
Mike Hofer <kc********@gmail.com> wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
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.


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).

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

P: n/a
Mike Hofer <kc********@gmail.com> wrote:

<snip>
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).

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

This discussion thread is closed

Replies have been disabled for this discussion.