Solved

Get Private Variables using Reflection in VB.NET (URGENT)

Posted on 2006-11-09
5
418 Views
Last Modified: 2012-05-05
Hi,

I'm not sure if this is possible but here goes...

I know you can use reflection to get the public properties of the current object/class:

      Me.GetType.getProperties

But I would like to get the private variables that are defined at the top of the class, see simple example below:



Public Class mySimpleClass

    Private _FirstName As String
    Private _LastName As String

    Public ReadOnly Property Fullname() As String
        Get
            Return _FirstName & " " & _LastName
        End Get
    End Property

    Public Sub New()

    End Sub

End Class



I know that example doesn't show much, but basically I know I could access the Property 'FullName' using reflection, however if I want to access the private variables _FirstName and _LastName, how would I go about it? If it's possile at all.
0
Comment
Question by:David_Reid_1985
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:Xeavn
ID: 17910386
Make them not private?

The whole point of declaring them private means that they can only be accessed and changed inside of the class. If you need to access those variables from outside the class then you want to switch the declaration to Public, or perhaps create a couple more public properties that allow you access to them.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17911452
Hi David_Reid_1985;

Yes that can be done. The following code assumes that the class mySimpleClass is part of the executing assembly.


        Dim SimpleClass As New mySimpleClass

        Dim myType As Type = GetType(mySimpleClass)
        Dim fieldInfos() As FieldInfo = myType.GetFields(BindingFlags.NonPublic Or _
            BindingFlags.Instance Or BindingFlags.Static)

        For Each fInfo As FieldInfo In fieldInfos
            Dim fieldName As String = fInfo.Name.PadRight(2)
            Dim fieldType As String = fInfo.FieldType.ToString
            Dim fieldValue As String = fInfo.GetValue(SimpleClass)
            Console.WriteLine("Field name = {0} Field value = {1} Field type = {2}", fieldName, fieldValue, fieldType)
        Next


Fernando
0
 

Author Comment

by:David_Reid_1985
ID: 17912674
I want to access them from within that class, so in the dispose function I can loop through all private variables setting them to nothing, rather than having to hard code the names of every variable.

Cheers Fernando I will try you suggestion later on today and let you know.

Thanks
David
0
 

Author Comment

by:David_Reid_1985
ID: 17912694
Just tried it, thanks Fernando worked a treat!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17913862
No problem, glad I was able to help. ;=)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question