Group,
I have a question about Reflection. I have created two structucture in a module, both accessor of public. i've created a class with one variable called 'v', i'd like to return this v as one of the structure depending on the parameter passed 'theType' but some how when the value is returned to the assinged variable it returns the error message
The error is: Unable to cast object of type 'System.Object[]' to type 's_FirstStructure[]'.
so when the function is called like below, the return value errors out on the assignment
s_tmpStructure = clsReflect.CreateVariable(New s_FirstStructure, dt)
i've tried in the function: return ctype(v,theType.GetType())
but states'theType.GetType()' is not defined, also tried it in the calling statement:
ctype(clsReflect.CreateVariable(New s_FirstStructure, dt), s_FirstStructure())
also tried directcast.
still, no luck. is there a way to change the type using reflection at runtime?
I don't fully understand the reflection, but i know how to access MemberInfo, PropertyInfo. Though do not know how to convert the object from Object Type to another type.
Please help.
Ronin Breank
p/s: below is the function in the class that creates the variable.
Code Block
Public Function CreateVariable(Optional ByVal theType As Object = Nothing, Optional ByVal dt As DataTable = Nothing)
If dt Is Nothing Then : Dim ta As New Application_SecurityTableAdapters.MyTableTableAdapter : dt = ta.GetData : End If
ReDim v(dt.Rows.Count - 1)
For intC As Integer = 0 To dt.Rows.Count - 1
v(intC) = theType
For Each c As DataColumn In dt.Columns
For Each f As FieldInfo In v(intC).GetType().GetFields()
If f.Name.Equals(c.ColumnName) Then
Dim vt As ValueType = v(intC)
f.SetValue(vt, dt.Rows(intC)(c))
v(intC) = vt
Exit For
End If
Next
Next
Next
Return v
End Function