vb.net和反射,需要从类名字符串中获取类型

发布于 2024-12-23 09:58:12 字数 830 浏览 2 评论 0原文

我正在构建一个验证框架,想要在数据库表中存储字段名称和针对这些字段名称的验证。

我想要做的是,给定表名(作为字符串),使用反射来实例化特定字段的 get 方法(使用 Get 和 Set 方法定义为属性。

以及一个记录包装器中的 get/set 对的示例。

'------------------------------------------------------------------------------------------------
'---------Get/Set (for field: VC60 
'------------------------------------------------------------------------------------------------
'

Public Property VC60() As String

    Get
        Return _VC60
    End Get


    Set(ByVal value As String)
        _VC60 = value
    End Set

End Property

我可以使用

    Dim t As Type = GetType(DB_TableName)

获取类型并使用 t.GetMembers 获取方法,

但我缺少的是如何在给定类的字符串名称的情况下获取类的类型

这是一个使用 Visual 生成 .exe 的控制台应用程序。基本2008年

我有尝试使用 System.Activator.CreateInstance 访问程序集引用,但没有成功。

I am building a validation framework that wants to store field names and validations against those field names in a DB table.

What I wanted to do was, given the table name (as string), use reflection to instantiate the get method for specific fields (defined as properties with Get and Set methods.

And example of a get/set pair in one record wrapper.

'------------------------------------------------------------------------------------------------
'---------Get/Set (for field: VC60 
'------------------------------------------------------------------------------------------------
'

Public Property VC60() As String

    Get
        Return _VC60
    End Get


    Set(ByVal value As String)
        _VC60 = value
    End Set

End Property

I can use

    Dim t As Type = GetType(DB_TableName)

To get the type and use t.GetMembers to get the methods,

but the peice I am missing is how to get the type of the class given the string name of the class.

This is a console application producing a .exe using visual Basic 2008

I have tried accessing a assembly reference with System.Activator.CreateInstance without success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-12-30 09:58:12

GetType 可以接受字符串作为类型名称的参数。确保将命名空间限定的类型名称传递到 GetType 方法中,并在项目中提供该程序集。否则,您只会得到空返回值。

MSDN:GetType 方法(字符串)

GetType can accept a string as an argument for the type name. Ensure you're passing a namespace-qualified type name into your GetType method and have that assembly available in your project. Otherwise, you'll just get a null return value.

MSDN: GetType Method (String)

苍暮颜 2024-12-30 09:58:12

如果类型位于 mscorlib 中,则可以调用 Type.GetType(someString)

否则,您需要找到该类型的 Assembly 实例并调用其 GetType(string) 方法。

If the type is in mscorlib, you can call Type.GetType(someString).

Otherwise, you'll need to find the Assembly instance for the type and call its GetType(string) method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文