如何在VB 2010中动态创建对象和/或属性?

时间:2012-07-23 18:16:28

标签: vb.net

我正在尝试动态创建一个对象和/或属性......这可能吗?

CLIENT_INFO
  NAME
    F_NAME (= "LoriBeth")
    L_NAME (= "Johnson")
  ADDRESS
    ADDY1 (= "123 MyStreet")
    ETC (= "Etc")

其中:

Dim objCreated as Object
Dim objName AS String = "CLIENT_INFO"
Dim 1stProp as String = "NAME"
etc...

objCreated = makeObject(objName, objProp1, etc...)

Function makeObject(byval objName as String, byval objProp1 as String, etc...)
  Class &objName
    Property &objProp1
    End Property
  End Class

  Return &objName
End Function

(我使用VFP方式在运行时将vars替换为值)

最终得到一个可寻址的对象,如:

CLIENT_INFO.NAME.F_NAME =“LoriBeth”

2 个答案:

答案 0 :(得分:0)

您可以创建一个类型为Dictionary(Of String, Object)的单个属性来完成您的expando功能。

但这整件事看起来真的很糟糕。

答案 1 :(得分:0)

汉斯,这很酷,很高兴知道,但不允许我做我最初发布的内容。即使是这样的事情也会更有帮助:

Option Strict Off 
Imports System.Dynamic  
Module Module1     
   Sub Main()         
      Dim OldBag As Object = New ExpandoObject         
      OldBag.Name = "Hans"         
      OldBag.Age = 21         
      OldBag.NewName = "NewBag"
      Console.WriteLine(NewBag.Name)         
      Console.WriteLine(NewBag.Age)
   End Sub 
End Module