[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.sdk

Inherited private fields via reflection?

(Paul Hatcher)

12/17/2002 12:55:00 PM

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


1 Answer

(Felix Wu [MS])

12/19/2002 7:35:00 AM

0

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