Need some help. This example if from the Language Guide, (PropertyInfo.Value).
I have modified it slightly to set the ObjArray= ObjArray but that fails:
[code]Dim s() As Object
s = pi.Value(t)
pi.Value(t)=s //Here’s my problem
[/code]
TestClass looks like this:
Property TestArray() as string
Property ObjArray() as TestClass
Some experimenting has be believing that I need to cast the s() As Object to s() As TestClass.
But when I do that the type of ObjArray is TestClass() when it normally is Object() (When doing introspection of the property ObjArray).
My question probably is: How do I set a property to an array of classes with introspection?
[code]Dim t As New TestClass
t.TestArray.Append(“Text”)
t.TestArray.Append(“Some more text”)
t.ObjArray.Append new TestClass
t.ObjArray.Append new TestClass
Dim ti As Introspection.TypeInfo
ti = Introspection.GetType(t)
Dim p() As Introspection.PropertyInfo
p = ti.GetProperties
For Each pi As Introspection.PropertyInfo In p
Dim pt As Introspection.TypeInfo
pt = pi.PropertyType
Select Case pi.Name
Case "TestArray"
If pt.IsArray Then
If pi.Value(t).ArrayElementType = 8 Then // String
Dim s() As string
s = pi.Value(t)
MsgBox("Array = (" + Join(s, ",") + ")")
End If
End If
Case "ObjArray"
If pt.IsArray Then
Dim s() As Object
s = pi.Value(t)
break
pi.Value(t)=s //Here's my problem
end if
End Select
The problem is upcasting vs. downcasting. You can downcast an object into its super class but not the other way around.
In your case, since you know the type of array, you can do this:
dim s() as TestClass = pi.Value( t )
pi.Value( t ) = s
or
dim s() as object = pi.Value( t )
redim s( -1 )
s.Append new TestClass
s.Append new TestClass
// No need to assign it back since it's the array that
// was stored in the property originally
from sub to super is “upcast” (if you imagine a tree that is rooted at the top)
from super into sub is “downcast” (again if you imagine the top of the hierarchy is at the top)