6

Is it possible using Reflection or some other method to obtain a reference to a specific class instance from the name of that class instance?

For example the framework for the applications i develop heavily uses public class instances such as: Public bMyreference as MyReference = new MyReference

Then throughout the application bMyReference is used by custom controls and code.

One of the properties of the custom controls is the "FieldName" which references a Property in these class instances (bMyReference.MyField) as a string.

What i would like to be able to do is analyze this string "bMyReference.MyField" and then refer back to the actual Instance/Property.

In VB6 I would use an EVAL or something simular to convert the string to an actual object but this obviously doesn't work in VB.net

What I'm picturing is something like this

Dim FieldName as String = MyControl.FieldName ' sets FielName to bMyReference.MyField

Dim FieldObject() as String = FieldName.Split(".") ' Split into the Object / Property

Dim myInstance as Object = ......... ' Obtain a reference to the Instance and set as myInstance

Dim myProperty = myInstance.GetType().GetProperty(FieldObject(1))
2
  • 1
    If it is possible, I wouldn't even want to tell you how to do it, because this leads to an architecture which is very poorly maintainable, as well as pretty slow. There are much better Object Oriented architectures that would allow you to create your application in a more suitable way. Commented Mar 12, 2010 at 17:31
  • A specific class instance, or an instance of a specific type? They are different things. Perhaps VB6 is screwing up your idea of OO classes and properties. Commented Mar 12, 2010 at 17:40

1 Answer 1

8

I don´t know if I´ve understood you well, but my answer is yes, you can do it by reflection. You´ll need to import System.Reflection namespace.

Here is an example:

    ' Note that I´m in namespace ConsoleApplication1
    Dim NameOfMyClass As String = "ConsoleApplication1.MyClassA"
    Dim NameOfMyPropertyInMyClass As String = "MyFieldInClassA"

    ' Note that you are getting a NEW instance of MyClassA
    Dim MyInstance As Object = Activator.CreateInstance(Type.GetType(NameOfMyClass))

    ' A PropertyInfo object will give you access to the value of your desired field
    Dim MyProperty As PropertyInfo = MyInstance.GetType().GetProperty(NameOfMyPropertyInMyClass)

Once you have MyProperty, uou can get the value of your property, just like this:

MyProperty.GetValue(MyInstance, Nothing)

Passing to the method the instace of what you want to know the value.

Tell me if this resolve your question, please :-)

EDIT

This would be ClassA.vb

Public Class MyClassA

    Private _myFieldInClassA As String

    Public Property MyFieldInClassA() As String
        Get
            Return _myFieldInClassA
        End Get
        Set(ByVal value As String)
            _myFieldInClassA = value
        End Set
    End Property

End Class
Sign up to request clarification or add additional context in comments.

1 Comment

Just a quick note for ASP.Net devs who might be having trouble. If your "ClassA" is in an App_Code vb file, but you're trying to create the class instance in your aspx/ascx code, Type.GetType() will return null. I had to create a new function in another App_Code vb file that takes the class name as an argument, and returns Type.GetType(NameOfMyClass). I'm not totally clear on exactly how everything works, but my guess is that the aspx/ascx code is in a different assembly than the App_Code files, so GetType can't find the class. Please correct me if there's a simpler/better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.