0

クラス名に基づいてクラスの 1 つのインスタンスを作成する次のコードがあります。

'Get a System.Type corresponding to the name of the parent class  
Dim tt As Type = Type.GetType(item.ParentClass)
'Create an instance of the specified type using the types default constructor
Dim theObject As Object = Activator.CreateInstance(tt)

のように完全なクラスがある場合、これは正常に機能しますが、これを justで MainClassName.Namespace.ClassName 実行しようとすると、何もありません。ClassNamett

ClassName次のように完全修飾名を使用して取得する方法はありますかMainClassName.Namespace.ClassName

4

2 に答える 2

0

次のようにType.AssemblyQualifiedNameを使用する必要があります。

'part 1 of your code - store class name in a universal format
Dim className As String = GetType(item.ParentClass).AssemblyQualifiedName

'part 2 of your code - use a previously stored class name to create instance
Dim tt As Type = Type.GetType(className)
Dim theObject As Object = Activator.CreateInstance(tt)

クレジットはSO のこの回答に送られます。

于 2013-04-16T20:36:02.610 に答える