昏暗的变量的数据类型存储在数据库领域 [英] Dim variable as datatype stored in db field

查看:74
本文介绍了昏暗的变量的数据类型存储在数据库领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有一个名为数据类型字段的数据库表。它将保留特定的数据类型,如字符串,整数,类名,等等...

I have database table which has a field called "datatype". It will hold a particular datatype, such as string, integer, classname, etc....

我如何使用暗淡无论是存储在这一领域的变量?例如,如果该字段说弦我想有效地说点心作为MyVar的字符串,但什么是存储在表中的弦部分填写。希望是有道理的。谢谢!

How can I dim a variable using whatever is stored in this field? For example, if the field says "string" I want to effectively say "Dim MyVar as string", but fill in the "string" portion with what's stored in the table. Hope that makes sense. Thanks!

推荐答案

您可以在类的名称创建实例。注意,你不能没有它面前的命名空间索要MyClass1的。有从MyClass1的到Namespace.MyClass1,如字典获得,甚至在你的数据库把完整的类型名称某些选项。

You can create instances from the name of the class. Note, you can't ask for "MyClass1" without the namespace before it. There are some options for getting from "MyClass1" to "Namespace.MyClass1" such as a Dictionary or even putting the full type name in your database.

Module Module1
    Sub Main()
        ' compiler knows mc1 is a IMyClasses
        Dim mc1 = CType(getInstanceFromTypeName("ConsoleApplication1.MyClass1"), IMyClasses)
        ' compiler doesn't know, mc2 is an object
        Dim mc2 = getInstanceFromTypeName("ConsoleApplication1.MyClass2")
        mc1.Foo()
        mc2.foo()
    End Sub
    Private Function getInstanceFromTypeName(typeName As String) As Object
        Dim o As Object
        Try
            o = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(typeName)
        Catch ex As Exception
            o = Nothing
        End Try
        Return o
    End Function
End Module

Public Class MyClass1
    Implements IMyClasses
    Public Sub Foo() Implements IMyClasses.Foo
        Debug.Print("MyClass1")
    End Sub
End Class

Public Class MyClass2
    Implements IMyClasses
    Public Sub Foo() Implements IMyClasses.Foo
        Debug.Print("MyClass2")
    End Sub
End Class

Public Interface IMyClasses
    Sub Foo()
End Interface

mc1.Foo()的作品,因为MC1被声明为IMyClasses和IMyClasses定义这个子程序。编译器知道IMyClasses定义符。

mc1.Foo() works because mc1 is declared as an IMyClasses, and IMyClasses defines this subroutine. The compiler knows that IMyClasses defines Foo.

mc2.foo()不与选项严格,因为美孚()不是对象的成员工作。随着O.S.On,编译器必须能够解决在编译时所有的函数调用。它的工作原理与选项严格关不过,由于O.S.Off允许在对象函数调用,但有可能是危险的,因为O.S.Off还允许mc2.asdf()为例。

mc2.foo() doesn't work with Option Strict On because Foo() is not a member of Object. With O.S.On, the compiler must be able to resolve all function calls at compile time. It works with Option Strict Off however, as O.S.Off allows function calls on Object, but can potentially be dangerous because O.S.Off also allows mc2.asdf(), for example.

其他资源:

使用的System.Reflection

使用System.Activator

这篇关于昏暗的变量的数据类型存储在数据库领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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