问题描述
我想通过反射来设置属性的值.在此线程中,提出解决方案.但是解决方案的问题在于它没有实例化属性.但是如果需要,我想检查并实例化属性.我的DTO是:
I want to set the values of the properties via reflection. In this thread they propose a solution. But the problem with the solution is that it is not instantiating the properties. But I want to check and instantiate the properties if necessary. My DTO is:
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
现在用于设置属性的我已经定义了以下功能:
Now for setting the properties I have defined the following function:
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
,那么它将正常工作:
If before calling SetProperty(configObject,...)
I instantiate configObject.Printing
then it will work fine:
Dim configObject as New root
configObject.Printing = new rootPrinting()
SetProperty(configObject , "Printing.Printer","skjfkd")
否则,调用SetProperty(...)
后,configObject.Printing
将为Nothing
.
似乎在调用Activator.CreateInstance(propertyToGet.PropertyType)
时,对原始对象的引用会丢失.当函数中的对象真正被初始化时,主对象仍为Nothing
.如何正确实例化class属性?
Otherwise after calling SetProperty(...)
, configObject.Printing
will be Nothing
.
It seems that when calling Activator.CreateInstance(propertyToGet.PropertyType)
the reference to the original object is lost. While the object in the function is really initialized, the main object remains Nothing
. How can I instantiate the class property correctly?
推荐答案
好.问题解决了.为了解决该问题,必须对代码进行如下修改:
Ok. The problem was solved. To solve the problem the code has to be modified as following:
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))
Dim property_value = propertyToGet.GetValue(target, Nothing)
If IsNothing(property_value) Then
If propertyToGet.PropertyType.IsClass Then
property_value = Activator.CreateInstance(propertyToGet.PropertyType)
propertyToGet.SetValue(target, property_value)
End If
End If
target = property_value
Next
Dim propertyToSet As PropertyInfo = target.GetType().GetProperty(properties.Last())
propertyToSet.SetValue(target, value)
End Sub
这篇关于VB.NET:通过反射实例化嵌套的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!