为什么不将对象的数据类型传递给函数?你是如何解决它的?
Dim MyObj as new CustomObj
Dim t As Type = MyObj.GetType
Call My_Fuction(Of t)
我将可序列化的对象保存到文件中,然后稍后打开它们,然后代码需要根据对象数据类型找到UI,这样它就可以从对象中填充UI
Private Function My_Fuction(Of t As Base_Object)() As UserControl
Dim UI_Type As Type = GetType(UI_Common_Panel(Of t))
For Each Object_type As Type In Project_Solution.GetTypes()
For Each Itype As Type In Object_type.GetInterfaces()
If Itype Is UI_Type Then Return DirectCast(Activator.CreateInstance(Object_type), UI_Common_Panel(Of t))
Next
Next
Return Nothing
End Function
答案 0 :(得分:0)
很难给出一个好的答案,因为你的问题中有所有自定义类。如果您要解决的问题是基于对象的Type
创建新控件,可以使用内置控件执行此操作:
Function GetControl(o As Object) As Control
If o.GetType Is GetType(Boolean) Then
Return New CheckBox With {.Checked = DirectCast(o, Boolean)}
ElseIf o.GetType Is GetType(Date) Then
Return New DateTimePicker With {.Value = DirectCast(o, Date)}
ElseIf o.GetType Is GetType(String) Then
Return New TextBox With {.Text = DirectCast(o, String)}
Else
Return New TextBox With {.Text = o.ToString}
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim c As Control = GetControl("Hello, world!")
Me.Controls.Add(c)
c.Visible = True
Dim c2 As Control = GetControl(#05/04/2017#)
Me.Controls.Add(c2)
c2.Visible = True
c2.Top = 100
End Sub