473,405 Members | 2,344 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,405 software developers and data experts.

Reflection and access-modifiers

Hi all,

Using reflection, I can invoke/call private methods of an object.

is this intended?
if yes, why? in what scenario (example would be good) should I be givven the
option to use something that was declared private (not public) and not mine
to use? seems to me this is somewhat malicious...
if not.... well....

Thanx,
Picho
Nov 16 '05 #1
4 3187
Yes its intended - its how the System.Runtime.Serialization infrastructure works for example

Regards

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

Hi all,

Using reflection, I can invoke/call private methods of an object.

is this intended?
if yes, why? in what scenario (example would be good) should I be givven the
option to use something that was declared private (not public) and not mine
to use? seems to me this is somewhat malicious...
if not.... well....

Thanx,
Picho

Nov 16 '05 #2
Yes, this is intentional and it's necessary for a number of aspects of .NET
to function properly.

Remember, things like private and protected are generally language specific
issues, so you could very well write a language that didn't use access
modifiers and didn't respect them in the framework or other .NET software.
On the other hand, I wouldn't recommend using it willy-nilly.

There have actually been times where I've used it to bypass what I would
consider bad design choices by MS, to get at functionality in the underlying
framework that I otherwise couldn't. But I would not expect that code to
work in future versions of .NET, so consider doing stuff like that to be
non-portable and generally unsafe.

Pete

"Picho" <SP********@telhai.ac.il> wrote in message
news:Os****************@TK2MSFTNGP15.phx.gbl...
Hi all,

Using reflection, I can invoke/call private methods of an object.

is this intended?
if yes, why? in what scenario (example would be good) should I be givven the option to use something that was declared private (not public) and not mine to use? seems to me this is somewhat malicious...
if not.... well....

Thanx,
Picho

Nov 16 '05 #3
Pete Davis <pd******@NOSPAM.hotmail.com> wrote:
Yes, this is intentional and it's necessary for a number of aspects of .NET
to function properly.

Remember, things like private and protected are generally language specific
issues, so you could very well write a language that didn't use access
modifiers and didn't respect them in the framework or other .NET software.


I don't believe that's true - unless you are running in a full trust
environment.

While there are some access modifiers (I can't remember which off-hand,
unfortunately) which are more "advisory" than compulsory, I believe
most will be enforced by the CLR, at least when running in anything
other than full trust, and especially with verification on.

Note that without ReflectionPermission, you can't access non-public
methods via reflection either.

--
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
I apologize, I should have been more clear. My meaning was, one could create
a language that ignored access modifiers by using reflection to call methods
that would otherwise be prohibited. You are correct, of course, that the CLR
will enforce access modifiers and that you'd need reflection permissions.

This has no been my day. My head has been in a cloud since I woke up. I
apologize for not being more clear.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pete Davis <pd******@NOSPAM.hotmail.com> wrote:
Yes, this is intentional and it's necessary for a number of aspects of ..NET to function properly.

Remember, things like private and protected are generally language specific issues, so you could very well write a language that didn't use access
modifiers and didn't respect them in the framework or other .NET
software.
I don't believe that's true - unless you are running in a full trust
environment.

While there are some access modifiers (I can't remember which off-hand,
unfortunately) which are more "advisory" than compulsory, I believe
most will be enforced by the CLR, at least when running in anything
other than full trust, and especially with verification on.

Note that without ReflectionPermission, you can't access non-public
methods via reflection either.

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

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

Similar topics

0
by: Brian Loesgen | last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25, 2003 at the Scripps Ranch Library. Scripps Ranch Library 10301 Scripps Lake Drive San Diego, CA 92131-1026 Please join us...
2
by: UJ | last post by:
Can somebody point me to an explanation of what Reflection is? I've tried looking in the help and it's not very helpful. TIA - Jeff.
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...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
3
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
by: Christopher Burns via .NET 247 | last post by:
Hi gang, I am trying to access the controls on an .ascx via reflection, but cannot seem to figure out which of the "Get..." methods on the type to call to do it. I have a page with a textbox...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.