反思 - 类中的数组的SetValue? [英] Reflection - SetValue of array within class?
问题描述
OK,我一直在努力的东西,而现在,使用反射来完成的很多事情,我需要做的,但我已经打了一个有点绊脚石......
OK, I've been working on something for a while now, using reflection to accomplish a lot of what I need to do, but I've hit a bit of a stumbling block...
我试图使用反射来填充一个子类属性的阵列的属性...不知道是十分明显的,因此它可能是最好的code解释:
I'm trying to use reflection to populate the properties of an array of a child property... not sure that's clear, so it's probably best explained in code:
父类:
Public Class parent
Private _child As childObject()
Public Property child As childObject()
Get
Return _child
End Get
Set(ByVal value As child())
_child = value
End Set
End Property
End Class
子类:
Public Class childObject
Private _name As String
Public Property name As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _descr As String
Public Property descr As String
Get
Return _descr
End Get
Set(ByVal value As String)
_descr = value
End Set
End Property
End Class
因此,使用反射,我试图建立孩子的数组的值通过父对象的对象...
So, using reflection, I'm trying to set the values of the array of child objects through the parent object...
我试过几种方法...以下是pretty多少我此刻已经有了(我已经添加了样本数据只是为了让事情变得简单):
I've tried several methods... the following is pretty much what I've got at the moment (I've added sample data just to keep things simple):
Dim Results(1) As String
Results(0) = "1,2"
Results(1) = "2,3"
Dim parent As New parent
Dim child As childObject() = New childObject() {}
Dim PropInfo As PropertyInfo() = child.GetType().GetProperties()
Dim i As Integer = 0
For Each res As String In Results
Dim ResultSet As String() = res.Split(",")
ReDim child(i)
Dim j As Integer = 0
For Each PropItem As PropertyInfo In PropInfo
PropItem.SetValue(child, ResultSet(j), Nothing)
j += 1
Next
i += 1
Next
parent.child = child
这悲惨的失败与PropItem.SetValue的ArgumentException:未找到属性设置方法
This fails miserably on PropItem.SetValue with ArgumentException: Property set method not found.
任何人有什么想法?
@乔恩: -
谢谢,我想我已经得到远一点,通过创建单独的子对象,然后将它们分配到一个数组...这个问题现在试图获得(使用反射)分配给父对象数组。
Thanks, I think I've gotten a little further, by creating individual child objects, and then assigning them to an array... The issue is now trying to get that array assigned to the parent object (using reflection).
它不应该是困难的,但我认为问题就来了,因为我不一定知道父/子类型。我使用反射来确定哪些父/子被传递。父总是只有一个属性,它是子对象的阵列。当我尝试分配子阵列父对象,我得到一个无效转换异常说,它不能转换对象[]来。
It shouldn't be difficult, but I think the problem comes because I don't necessarily know the parent/child types. I'm using reflection to determine which parent/child is being passed in. The parent always has only one property, which is an array of the child object. When I try assigning the child array to the parent object, I get a invalid cast exception saying it can't convert Object[] to .
编辑:
的基本上,我现在拥有的是:的
Dim PropChildInfo As PropertyInfo() = ResponseObject.GetType().GetProperties()
For Each PropItem As PropertyInfo In PropChildInfo
PropItem.SetValue(ResponseObject, ResponseChildren, Nothing)
Next
ResponseObject是父类的一个实例,并ResponseChildren是childObject类的数组。
ResponseObject is an instance of the parent Class, and ResponseChildren is an array of the childObject Class.
这失败:
类型的对象'System.Object的[]'不能转换为类型'childObject []'。
This fails with: Object of type 'System.Object[]' cannot be converted to type 'childObject[]'.
推荐答案
首先我想摆脱方程的阵列部分 - 我看不出这是相关的。试着写code为一的单的孩子设置的值。
Firstly I'd get rid of the array part of the equation - I can't see how that's relevant. Try to write code to set the values for a single child.
其次,看来你是靠的GetProperties
的所需顺序是结果 - 你不应该。有没有保证,以什么顺序的属性将被退回。你应该知道你根据你分割字符串想要什么样的顺序,并获取属性的按名称的
Secondly, it seems that you're relying on the results of GetProperties
being in the desired order - you shouldn't. There's no guarantee as to what order the properties will be returned in. You should know what order you want based on the string you're splitting, and fetch the properties by name.
第三,我的犯罪嫌疑人的问题是,你有一些只读属性以及可写的。我建议你掀起一个短的控制台应用程序来检查了这一点,登录你想你设置它之前设置了什么属性。
Thirdly, I suspect the problem is that you've got some read-only properties as well as writeable ones. I suggest you whip up a short console app to check this out, logging what properties you're trying to set before you set it.
如果这没有帮助,请发表一个简短而完整的控制台应用程序,这表明了问题,我敢肯定,我们就可以解决这个问题。
If this doesn't help, please post a short but complete console app which demonstrates the problem, and I'm sure we'll be able to fix it.
编辑:好吧,如果你现在只是停留在阵列部分,我建议你看的是的短,但完整的例子来代替。我怀疑的问题是,你所创建的错误类型的数组。您可以使用Array.CreateInstance创造合适类型的数组,这应该是有效的,当你再设置属性。
Okay, if you're now stuck just on the array part, I suggest you show a short but complete example of that instead. I suspect the problem is that you've created an array of the wrong type. You can use Array.CreateInstance to create the right kind of array, which should be valid when you then set the property.
这篇关于反思 - 类中的数组的SetValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!