我想通过反射来设置属性的值。在这
thread
他们提出了解决办法。但解决方案的问题是它没有实例化属性。但如果需要,我想检查并实例化这些属性。我的DTO是:
Public Class root
Public Property Printing() As rootPrinting
End Class
Public Class rootPrinting
Public Property Printer() As String
Public Property PrinterBatch() As String
End Class
现在,为了设置属性,我定义了以下函数:
Public Sub SetProperty(ByVal target As Object, ByVal compoundProperty As String, ByVal value As Object)
Dim properties As String() = compoundProperty.Split("."c)
For i As Integer = 0 To properties.Length - 1 - 1
Dim propertyToGet As PropertyInfo = target.[GetType]().GetProperty(properties(i))
target = propertyToGet.GetValue(target, Nothing)
if IsNothing(target) then
if propertyToGet.PropertyType.IsClass then
target = Activator.CreateInstance(propertyToGet.PropertyType)
End If
End If
Next
Dim propertyToSet As PropertyInfo = target.[GetType]().GetProperty(properties.Last())
propertyToSet.SetValue(target, value, Nothing)
End Sub
然后我这样称呼它:
Dim configObject as New root
SetProperty(configObject , "Printing.Printer","skjfkd")
如果在呼叫前
SetProperty(configObject,...)
我实例化
configObject.Printing
然后它会很好地工作:
Dim configObject as New root
configObject.Printing = new rootPrinting()
SetProperty(configObject , "Printing.Printer","skjfkd")
打过电话之后
SetProperty(...)
,
配置对象打印
将
Nothing
.
好像打电话的时候
Activator.CreateInstance(propertyToGet.PropertyType)
对原始对象的引用将丢失。当函数中的对象真正初始化时,主对象仍保留
没有什么
. 如何正确实例化类属性?