Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Reflection
 

Reflection

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

RoninBreank  Monday, October 15, 2007 8:52 PM

Group,

Thanks for viewing the question... but nobugz have answered it. here is the link to the other forum.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2276634&SiteID=1&mode=1

RoninBreank  Tuesday, October 16, 2007 2:06 PM

Group,

Thanks for viewing the question... but nobugz have answered it. here is the link to the other forum.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2276634&SiteID=1&mode=1

RoninBreank  Tuesday, October 16, 2007 2:06 PM

You can use google to search for other answers

Custom Search

More Threads

• UITypeEditor cannot find dependency dlls that are not in system32
• I can't see all form
• Controls disappearing from designer/runtime for no reason
• Creating Tree View in Datagrid in VB.Net 2005?
• member field for custom component not getting added to form
• How to highlight dates in Calendar control (WinForms)?
• RightToLeft support in PropertyGrid
• "base class <...> could not be loaded" when compiling with /clr
• Resize Control
• What's wrong with the designer ?