How to use Reflection to access private members?

D

DabblerNL

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

OmegaSquared

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
 
D

DabblerNL

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

OmegaSquared

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
 
D

DabblerNL

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

OmegaSquared

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
 
D

DabblerNL

Well , I found a solution of sorts, but it requires vstudio 2008 (or 2005
team system?):
[TestMethod]
public void TestSkyIsBlue()
{
var mySky=new Sky(Sun.Set)
var mySkyPrivateAccessor=new PrivateObject(mySky);
Assert.IsFalse((bool)mySkyPrivateAccessor.GetFieldOrProperty("IsBlue");
}

The solution surely uses reflection just as the code below does, but it does
not fail now.
 

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