Obtaining reference to Class instance by string name - VB.NET

Posted by Jeff Williams on Stack Overflow See other posts from Stack Overflow or by Jeff Williams
Published on 2010-03-12T17:24:01Z Indexed on 2010/03/12 18:37 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about reflection