[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework

reposted: accessing private members through reflection in vstudio

DabblerNL

11/7/2008 7:24: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?

(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)
6 Answers

Jeroen Mostert

11/7/2008 7:33:00 PM

0

DabblerNL 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?
>
> (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/librar...

--
J.
(Not an MVP)

Jeroen Mostert

11/7/2008 7:37:00 PM

0

Jeroen Mostert wrote:
> DabblerNL 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?
>>
>> (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/librar...
>
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.

--
J.

DabblerNL

11/7/2008 8:11:00 PM

0

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.




Jeroen Mostert

11/7/2008 8:57:00 PM

0

DabblerNL wrote:
> 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.reflectionperm...

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

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/CodeAccessSe...

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

--
J.

Family Tree Mike

11/8/2008 1:30:00 AM

0

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.

"DabblerNL" <Dabbler@communty.nospam> wrote in message
news:9F707367-361A-480B-97C9-B5EBAC9F6FD4@microsoft.com...
> 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.
>
>
>
>

Peter Duniho

11/8/2008 2:10:00 AM

0

On Fri, 07 Nov 2008 17:29:30 -0800, Family Tree Mike
<FamilyTreeMike@thisoldhouse.com> wrote:

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

Exactly. And in particular, an MSDN subscription does _not_ guarantee a
response from an MVP. It only guarantees a response from Microsoft :) (as
far as I know, Microsoft employees are not actually eligible for the MVP
award).