473,385 Members | 1,769 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,385 software developers and data experts.

How to access private members of a base class

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
10 25706
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
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
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
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

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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Andy Ward | last post by:
Given the following code: class A { protected: int pro; }; class B : public A { public:
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
13
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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,...

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.