reposted: accessing private members through reflection in vstudio

  • Thread starter Thread starter DabblerNL
  • Start date Start date
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?

(reposted because I have a MSDN subscription and while the responses given
are certainly helpful seem not to have originated form a MVP or something)
 
DabblerNL said:
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?

(reposted because I have a MSDN subscription and while the responses given
are certainly helpful seem not to have originated form a MVP or something)

Wait, you already *got* responses, are not discussing them here, and are now
waiting for an MVP to re-answer a question you could have answered yourself
with four minutes of reading the documentation? Doesn't this strike you in
any way as disingenuous?

Here's your unambiguously authoritative solution, coming from the MSDN
itself: http://msdn.microsoft.com/library/zy0d4103
 
Jeroen said:
Wait, you already *got* responses, are not discussing them here, and are
now waiting for an MVP to re-answer a question you could have answered
yourself with four minutes of reading the documentation? Doesn't this
strike you in any way as disingenuous?

Here's your unambiguously authoritative solution, coming from the MSDN
itself: http://msdn.microsoft.com/library/zy0d4103
Ahh, I've just stumbled on the thread you created earlier, in this very
newsgroup.

My answer is useless to you because you reposted your original question,
which you already got responses to, without explaining why your problem
still isn't fully solved.

You might want to reconsider your question asking strategies.
 
The link you give summarizes the discussion quite well, so thanks for that
part of your post.

The problem is indeed that (quoted from the msdn link):

"If the requested property is non-public and the caller does not have
ReflectionPermission to reflect non-public objects outside the current
assembly, this method returns a null reference (Nothing in Visual Basic)."

As I am testing in a test.dll against the application.dll, the test.dll
needs to have permission to access private members in the application.dll

I am currently trying to find out how to do this, but any help in providing
a "reflectionpermission for the uninitiated (or noobs in some other's
dictionary)" link would be appreciated.

As for reposting: I paid quite a lot of dollars for my MSDN subscription and
getting help from a microsoft professional within 48 hours is included when
posting in a managed newgroup. As I did not get it, I reposted after getting
an advice from the online concierge to do so. No offence was intended against
any other poster on this forum.
 
DabblerNL said:
The problem is indeed that (quoted from the msdn link):

"If the requested property is non-public and the caller does not have
ReflectionPermission to reflect non-public objects outside the current
assembly, this method returns a null reference (Nothing in Visual Basic)."

As I am testing in a test.dll against the application.dll, the test.dll
needs to have permission to access private members in the application.dll
You don't actually control reflection permission as narrowly as that. You
cannot grant specific permission for reflection on specific assemblies to
specific assemblies -- you give the assembly permission to perform (specific
kinds of) reflection based on grant sets. See
http://msdn.microsoft.com/library/system.security.permissions.reflectionpermissionflag

Normally, assemblies run in the context of your local machine, which
automatically gives them full trust (and hence reflection permission). In
more restrictive environments (like a reconfigured ASP.NET) you may need to
grant this permission manually. See, for example,
http://www.west-wind.com/WebLog/posts/5587.aspx

To configure zones and security in general, you use caspol.exe or
mscorcfg.exe. Here's a nice page on that, which also serves as an
introduction to Code Access Security:
http://home.hot.rr.com/graye/Articles/CodeAccessSecurity.htm
As for reposting: I paid quite a lot of dollars for my MSDN subscription and
getting help from a microsoft professional within 48 hours is included when
posting in a managed newgroup. As I did not get it, I reposted after getting
an advice from the online concierge to do so.
As I'm seeing this as a Usenet newsgroup, I'm wholly insulated from all of that.
No offence was intended against any other poster on this forum.

None taken, but reposting verbatim without reformulating your question is
never a good idea. You can't expect people (even paid-for Microsofties) to
trawl through past articles. At the very least you should provide a link to
Google Groups or similar where they can reread the discussion.
 
Hopefully MS does answer you. It appears that you are using the same name,
login, whatever, that you did before. My understanding is that there is a
login that is tied to your account with MSDN, and if you don't use the
particular account, then MS won't notice it. Further, to the best of my
knowledge, MVPs are great, but their answers are not tied to this service,
nor are they speaking for Microsoft.

I'm following this because I hate to think of how outside sources could
misuse private methods and properties in a fielded system.
 
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");
}

This solution must still use reflection, but this time it does not fail.
Many thanks to Alan's Development blog:
http://www.gangleri.net/2007/11/15/PrivateObjects.aspx
 
Back
Top