Access private enumeration using reflection

  • Thread starter Fredrik Strandberg
  • Start date
F

Fredrik Strandberg

Hi!

I would highly appreciate some help with how to access an enumeration
declared as private in a class from the outside of the class.

This is a simplified example of the situation:

Const CONSTANT_1 As Integer = 4
Const CONSTANT_2 As Integer = 7

Public Class aClass

Private Enum myEnum
first = CONSTANT_1
second = CONSTANT_2
third
End Enum

End Class

Module Test
Sub Main()

Dim myClassObject As aClass = New aClass
Dim classType As Type = myClassObject.getType()

Dim bf As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Static
Dim m As MemberInfo = classType.GetMember("myEnum", bf)(0)

System.Enum.Parse(m.getType(), "third")

End Sub
End Module

I think my problem is how to convert the MemberInfo into a Enum object.
My goal is to acces the "third" constant in myEnum. The type returned
by MemberInfo is "RuntimeType", which Enum.parse does not allow. Is
there a way to convert my Type to an Enum? Is my approach all wrong?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top