通过字符串名称获取对Class实例的引用-VB.NET [英] Obtaining reference to Class instance by string name - VB.NET

查看:227
本文介绍了通过字符串名称获取对Class实例的引用-VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Reflection或其他方法从该类实例的名称获取对特定类实例的引用?

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?

例如,我开发的应用程序框架大量使用公共类实例,例如: 将bMyreference公开为MyReference =新的MyReference

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

然后在整个应用程序中,自定义控件和代码将使用bMyReference.

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

自定义控件的属性之一是"FieldName",它以字符串的形式引用了这些类实例(bMyReference.MyField)中的属性.

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

我想做的是分析此字符串"bMyReference.MyField",然后再返回到实际的实例/属性.

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

在VB6中,我将使用EVAL或类似的东西将字符串转换为实际对象,但这显然在VB.net中不起作用

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

我想像的是这样的东西

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

推荐答案

我不知道我对你的理解是否很好,但是我的回答是,您可以通过反思来做到.您需要导入System.Reflection命名空间.

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.

这里是一个例子:

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

拥有MyProperty后,uou就可以获取您的财产的价值,就像这样:

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

编辑

这将是 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

这篇关于通过字符串名称获取对Class实例的引用-VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆