[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.sdk

RE: Inherited private fields via reflection?

(Paul Hatcher)

12/19/2002 9:54:00 AM

In article <9R7d1iypCHA.2580@cpmsftngxa09>,
felixwu@online.microsoft.com (Felix Wu(MS)) wrote:

> Hi Paul,
>
> Yes, but you need to call the GetField method on its parent type
> object. For example:
>
> Public Class Parent
> Private myField As String = "private string in Parent class"
> End Class
>
> Public Class Child : Inherits Parent
> End Class
>
> Dim myInstance As New Child()
> Dim t As Type = myInstance.GetType().BaseType
> Try
> Dim fi As FieldInfo = t.GetField("myField",
> BindingFlags.NonPublic Or BindingFlags.Instance)
> Console.WriteLine("Parent's Secret number is {0}.",
> fi.GetValue(myInstance))
> Catch ex As Exception
> Console.WriteLine(ex)
> End Try
>
> Please note that if the requested type 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).
>
> Hope this helps.
>
> Regards,
>
> Felix Wu
> =============
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
> --------------------
> >Content-Class: urn:content-classes:message
> >From: "Paul Hatcher" <phatcher@cix.co.uk>
> >Sender: "Paul Hatcher" <phatcher@cix.co.uk>
> >Subject: Inherited private fields via reflection?
> >Date: Tue, 17 Dec 2002 03:55:27 -0800
> >Lines: 29
> >Message-ID: <065701c2a5c3$308e8ed0$d6f82ecf@TK2MSFTNGXA13>
> >MIME-Version: 1.0
> >Content-Type: text/plain;
> > charset="iso-8859-1"
> >Content-Transfer-Encoding: 7bit
> >X-Newsreader: Microsoft CDO for Windows 2000
> >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> >Thread-Index: AcKlwzCOgU3Gzw4LT6Sc0d0f1mU34Q==
> >Newsgroups: microsoft.public.dotnet.framework.sdk
> >Path: cpmsftngxa06
> >Xref: cpmsftngxa06 microsoft.public.dotnet.framework.sdk:5418
> >NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
> >X-Tomcat-NG: microsoft.public.dotnet.framework.sdk
> >
> >Is it possible to retreive a private field in a child
> >class that was declared in the parent?
> >
> >For example, given
> >
> >Public Class Parent
> > Private myField As String
> >End Clas
> >
> >Public Class Child : Inherits Parent
> >End Class
> >
> >can I assign to myField in the context of Child via
> >reflection. I'm using reflection as follows
> >
> >Dim field As FieldInfo = objType.GetField(name, flags)
> >
> >name = name of the field I'm after
> >flags = BindingFlags.Public Or BindingFlags.NonPublic Or
> >BindingFlags.IgnoreCase Or BindingFlags.Instance
> >
> >If it's not possible directly, it is possible to 'walk'
> >the inheritance hierarchy?
> >
> >Regards
> >
> >Paul
> >
> >
> >
>
>
Thanks, supplementary question: can I enquire if I have reflection
permission?

Paul
1 Answer

(Felix Wu [MS])

12/19/2002 12:17:00 PM

0

Hi Paul ,

If the assemblies are loaded from the local disk, you have unlimited
permission by default.

If you want to check the permission, you can the create an object of
ReflectionPermission type imperatively and then call Demand method. If you
haven't this permissioin, security exception would occur.

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: phatcher@cix.co.uk (Paul Hatcher)
>Reply-To: phatcher@cix.co.uk
>Subject: RE: Inherited private fields via reflection?
>Date: Thu, 19 Dec 2002 08:54 +0000 (GMT Standard Time)
>Message-ID: <memo.20021219085405.1316B@terra.compulink.co.uk>
>References: <9R7d1iypCHA.2580@cpmsftngxa09>
>Newsgroups: microsoft.public.dotnet.framework.sdk
>NNTP-Posting-Host: lan-102.phatch.adsl.alcom.co.uk 212.47.82.102
>Lines: 1
>Path: cpmsftngxa06!TK2MSFTNGP08!TK2MSFTNGP09
>Xref: cpmsftngxa06 microsoft.public.dotnet.framework.sdk:5460
>X-Tomcat-NG: microsoft.public.dotnet.framework.sdk
>
>In article <9R7d1iypCHA.2580@cpmsftngxa09>,
>felixwu@online.microsoft.com (Felix Wu(MS)) wrote:
>
>> Hi Paul,
>>
>> Yes, but you need to call the GetField method on its parent type
>> object. For example:
>>
>> Public Class Parent
>> Private myField As String = "private string in Parent class"
>> End Class
>>
>> Public Class Child : Inherits Parent
>> End Class
>>
>> Dim myInstance As New Child()
>> Dim t As Type = myInstance.GetType().BaseType
>> Try
>> Dim fi As FieldInfo = t.GetField("myField",
>> BindingFlags.NonPublic Or BindingFlags.Instance)
>> Console.WriteLine("Parent's Secret number is {0}.",
>> fi.GetValue(myInstance))
>> Catch ex As Exception
>> Console.WriteLine(ex)
>> End Try
>>
>> Please note that if the requested type 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).
>>
>> Hope this helps.
>>
>> Regards,
>>
>> Felix Wu
>> =============
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>> --------------------
>> >Content-Class: urn:content-classes:message
>> >From: "Paul Hatcher" <phatcher@cix.co.uk>
>> >Sender: "Paul Hatcher" <phatcher@cix.co.uk>
>> >Subject: Inherited private fields via reflection?
>> >Date: Tue, 17 Dec 2002 03:55:27 -0800
>> >Lines: 29
>> >Message-ID: <065701c2a5c3$308e8ed0$d6f82ecf@TK2MSFTNGXA13>
>> >MIME-Version: 1.0
>> >Content-Type: text/plain;
>> > charset="iso-8859-1"
>> >Content-Transfer-Encoding: 7bit
>> >X-Newsreader: Microsoft CDO for Windows 2000
>> >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>> >Thread-Index: AcKlwzCOgU3Gzw4LT6Sc0d0f1mU34Q==
>> >Newsgroups: microsoft.public.dotnet.framework.sdk
>> >Path: cpmsftngxa06
>> >Xref: cpmsftngxa06 microsoft.public.dotnet.framework.sdk:5418
>> >NNTP-Posting-Host: TK2MSFTNGXA13 10.40.1.165
>> >X-Tomcat-NG: microsoft.public.dotnet.framework.sdk
>> >
>> >Is it possible to retreive a private field in a child
>> >class that was declared in the parent?
>> >
>> >For example, given
>> >
>> >Public Class Parent
>> > Private myField As String
>> >End Clas
>> >
>> >Public Class Child : Inherits Parent
>> >End Class
>> >
>> >can I assign to myField in the context of Child via
>> >reflection. I'm using reflection as follows
>> >
>> >Dim field As FieldInfo = objType.GetField(name, flags)
>> >
>> >name = name of the field I'm after
>> >flags = BindingFlags.Public Or BindingFlags.NonPublic Or
>> >BindingFlags.IgnoreCase Or BindingFlags.Instance
>> >
>> >If it's not possible directly, it is possible to 'walk'
>> >the inheritance hierarchy?
>> >
>> >Regards
>> >
>> >Paul
>> >
>> >
>> >
>>
>>
>Thanks, supplementary question: can I enquire if I have reflection
>permission?
>
>Paul
>