[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

How to use Reflection to access private members?

DabblerNL

11/4/2008 1:45:00 PM

I have a Sky class with the private property Isblue

I am trying to access this through reflection (NOT through the Sky_Accessor
class vstudio can create):
var mySky=new Sky();
var skyType=typeof(mySky);
var skyIsBlueProp=skyType.GetProperty("IsBlue");
var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();

the last line fails because skyIsBlueProp is null.
Changing the IsBlue property to public solves the issue.
How can I access IsBlue even when it is private?

12 Answers

Peter Duniho

11/4/2008 1:55:00 PM

0

On Tue, 04 Nov 2008 05:45:02 -0800, DabblerNL <Dabbler@communty.nospam>
wrote:

> I have a Sky class with the private property Isblue
>
> I am trying to access this through reflection (NOT through the
> Sky_Accessor
> class vstudio can create):
> var mySky=new Sky();
> var skyType=typeof(mySky);
> var skyIsBlueProp=skyType.GetProperty("IsBlue");
> var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();
>
> the last line fails because skyIsBlueProp is null.
> Changing the IsBlue property to public solves the issue.
> How can I access IsBlue even when it is private?

There are overloads in reflection that allow you to pass flags specifying
whether to get private members or not. You need to use one of those
overloads, and pass the appropriate flag(s).

Not that it's necessarily a good idea to be using reflection at all, never
mind to go around the accessibility restrictions put in place by a class.
But if you really, really have to...that's how you do it.

Pete

OmegaSquared

11/4/2008 8:41:00 PM

0

Hello, DabblerNL,

Try adding the parrameter "bindingAttr:=Reflection.BindingFlags.NonPublic"
after "IsBlue" in your GetProperty call. (Depending on how general you want
the search to be, you may want to include other BindingFlags as well.)

Cheers,
Randy



DabblerNL

11/4/2008 11:19:00 PM

0

Strange enough adding a BindingFlag argument breaks the test, even when using
a public property:

var skyIsBlueProp=skyType.GetProperty("IsBlue");
var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();

works fine as long as IsBlue is public.
Changing the code to:
var skyIsBlueProp=skyType.GetGetProperty("IsBlue",BindingFlags.Public)
causes skyIsBlueProp to become null!


"OmegaSquared" wrote:

> Hello, DabblerNL,
>
> Try adding the parrameter "bindingAttr:=Reflection.BindingFlags.NonPublic"
> after "IsBlue" in your GetProperty call. (Depending on how general you want
> the search to be, you may want to include other BindingFlags as well.)
>
> Cheers,
> Randy
>
>
>

Jack Jackson

11/5/2008 12:19:00 AM

0

Not strange. When you don't supply any BindingFlags, .NET must be
supplying some defaults. I would try adding BindingFlags.Instance.
The documentation says you must supply one of BindingFlags.Instance or
BindingFlags.Static to get a result.

<http://msdn.microsoft.com/en-us/library/zy0d410...

On Tue, 4 Nov 2008 15:19:00 -0800, DabblerNL <Dabbler@communty.nospam>
wrote:

>Strange enough adding a BindingFlag argument breaks the test, even when using
>a public property:
>
>var skyIsBlueProp=skyType.GetProperty("IsBlue");
>var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();
>
>works fine as long as IsBlue is public.
>Changing the code to:
>var skyIsBlueProp=skyType.GetGetProperty("IsBlue",BindingFlags.Public)
>causes skyIsBlueProp to become null!
>
>
>"OmegaSquared" wrote:
>
>> Hello, DabblerNL,
>>
>> Try adding the parrameter "bindingAttr:=Reflection.BindingFlags.NonPublic"
>> after "IsBlue" in your GetProperty call. (Depending on how general you want
>> the search to be, you may want to include other BindingFlags as well.)
>>
>> Cheers,
>> Randy
>>
>>
>>

OmegaSquared

11/5/2008 1:39:00 AM

0

Yes, Jack is correct. Sorry for only giving you part of the answer. That's
what happens when I rely on my memory. :-(

In a working example, I see that I have used:

Reflection.BindingFlags.IgnoreCase Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Public

for this purpose. If there is a possibility that the property is Static
(Shared in VB) then also include Reflection.BindingFlags.Static.

Cheers,
Randy

DabblerNL

11/7/2008 6:28:00 PM

0

Sorry guys,

ORing in the .Instance BindingFlag makes the
GetProperty(string,BindingFlags) work, while the property is public. Making
the property private once again gives the dreaded null reference exception
one line later.

Browsing I found some mention of how you have to apply security permissions
for external code to be allowed access to private members. I had hoped that
Vstudio had taken of care of that, but seemingly not.

OmegaSquared

11/7/2008 7:05:00 PM

0

Hello, DabblerNL,

I'm afraid I've been fortunate enough so far to have been able to ignore
security and permissions, so I can't help much with that.

Is it possible you are seeking a property declared in a base class from
which you have inherited the class in which you are seeking? (Did that make
sense?)

I recall that I had a problem retrieving a private field in such a case and
Mattias Sjögren helped me by pointing out that I had to use the DeclaringType
in order to retrieve it. I wonder if the same is true for Properties.

Cheers,
Randy

S'pion

1/19/2010 8:40:00 PM

0

Anne G a ?crit :
> Le 1/18/10 8:46 PM, Malachite a ?crit :
>> Anne G a ?crit :
>>> ------
>>> Une photo d'un bateau de croisi?re dont les passagers se pr?lassent ?
>>> la plage, jusque l? rien de choquant. Sauf que c'est ? Ha?ti que la
>>> sc?ne se d?roule. ? 60 miles ? peine, les survivants du s?isme
>>> manquent de tout.
>>> ------
>
>> Faudrait quand m?me pas emp?cher les vivants de vivre.
>
> J'aurais trouv? plus d?cent qu'ils d?tournent leur route.


ET faire perdre encore plus d'argent et de travail aux haitiens ?


>
> ? d?faut d'offrir l'asile ? des survivants s'ils voulaient absolument y
> aller, mais faut pas r?ver.




--

S'pion, un m?le n?cessaire.

Anne G

1/19/2010 9:19:00 PM

0

Le 1/19/10 1:40 PM, S'pion a ?crit :
> Anne G a ?crit :
>> Le 1/18/10 8:46 PM, Malachite a ?crit :
>>> Anne G a ?crit :
>>>> ------
>>>> Une photo d'un bateau de croisi?re dont les passagers se pr?lassent
>>>> ? la plage, jusque l? rien de choquant. Sauf que c'est ? Ha?ti que
>>>> la sc?ne se d?roule. ? 60 miles ? peine, les survivants du s?isme
>>>> manquent de tout.
>>>> ------
>>
>>> Faudrait quand m?me pas emp?cher les vivants de vivre.
>>
>> J'aurais trouv? plus d?cent qu'ils d?tournent leur route.

> ET faire perdre encore plus d'argent et de travail aux haitiens ?

Vous croyez vraiment que les Haitiens qui sont l? font beaucoup d'argent?

Les riches propri?taires du site, peut-?tre. Mais les employ?s...

> > ? d?faut d'offrir l'asile ? des survivants s'ils voulaient absolument y
> > aller, mais faut pas r?ver.
>
>
>
>

S'pion

1/19/2010 9:30:00 PM

0

Anne G a ?crit :
> Le 1/19/10 1:40 PM, S'pion a ?crit :
>> Anne G a ?crit :
>>> Le 1/18/10 8:46 PM, Malachite a ?crit :
>>>> Anne G a ?crit :
>>>>> ------
>>>>> Une photo d'un bateau de croisi?re dont les passagers se pr?lassent
>>>>> ? la plage, jusque l? rien de choquant. Sauf que c'est ? Ha?ti que
>>>>> la sc?ne se d?roule. ? 60 miles ? peine, les survivants du s?isme
>>>>> manquent de tout.
>>>>> ------
>>>
>>>> Faudrait quand m?me pas emp?cher les vivants de vivre.
>>>
>>> J'aurais trouv? plus d?cent qu'ils d?tournent leur route.
>
>> ET faire perdre encore plus d'argent et de travail aux haitiens ?
>
> Vous croyez vraiment que les Haitiens qui sont l? font beaucoup d'argent?


Bin, ils font le 2 $ par jour qu'ils ont besoin pour vivre ? Ces
touristes maritimes, peut-?tre ils ont donn? de meilleurs pourboires ou
achet?s plus de cossins ? Qui sait, Des dons m?mes. Il doit bien y
avoir quelques ha?tiens qui porte un chandail Gucchi, aujourd'hui et
pour le mois.

Est-ce que le pays au complet doit s'arr?ter pcq Port au Prince et sa
r?gion est sur le cul ? ( Il ?tait ou ce navire ? Haiti ? )

D'ailleurs, ce n'est pas le tourisme qui permet ? la R.D. et Cuba de
garder la t^te hors de l'eau ? Pourquoi Ha?ti n'a rien fait pour garder
ce filon d'argent ? ( c'est la faute aux grossistes j'imagine....)

Je l'ai d?j? dit, je ne pleure pas Ha?ti. Son peuple, oui.




--

S'pion, un m?le n?cessaire.