有两种服务可以帮助您在设计时发现和解决解决方案中的所有类型:
另一方面,要在属性编辑器的下拉列表中显示标准值,您可以创建一个TypeConverter:
了解上述选项后,您可以创建自定义类型转换器以发现项目中的所有表单类型并在下拉列表中列出。
示例
在下面的示例中,我创建了一个自定义按钮类,它允许您在设计类型中选择一个表单类型,然后在运行时,如果您单击该按钮,它会将所选表单显示为对话框:
要查看此答案的 C# 版本,请参阅 this post。
我的按钮
Imports System.ComponentModel
Public Class MyButton
Inherits Button
<TypeConverter(GetType(FormTypeConverter))>
Public Property Form As Type
Protected Overrides Sub OnClick(ByVal e As EventArgs)
MyBase.OnClick(e)
If Form IsNot Nothing AndAlso GetType(Form).IsAssignableFrom(Form) Then
Using f = CType(Activator.CreateInstance(Form), Form)
f.ShowDialog()
End Using
End If
End Sub
End Class
FormTypeConverter
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Globalization
Public Class FormTypeConverter
Inherits TypeConverter
Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function CanConvertTo(ByVal pContext As ITypeDescriptorContext, ByVal pDestinationType As Type) As Boolean
Return MyBase.CanConvertTo(pContext, pDestinationType)
End Function
Public Overrides Function ConvertTo(ByVal pContext As ITypeDescriptorContext, ByVal pCulture As CultureInfo, ByVal pValue As Object, ByVal pDestinationType As Type) As Object
Return MyBase.ConvertTo(pContext, pCulture, pValue, pDestinationType)
End Function
Public Overrides Function CanConvertFrom(ByVal pContext As ITypeDescriptorContext, ByVal pSourceType As Type) As Boolean
If pSourceType = GetType(String) Then Return True
Return MyBase.CanConvertFrom(pContext, pSourceType)
End Function
Public Overrides Function ConvertFrom(ByVal pContext As ITypeDescriptorContext, ByVal pCulture As CultureInfo, ByVal pValue As Object) As Object
If TypeOf pValue Is String Then Return GetTypeFromName(pContext, CStr(pValue))
Return MyBase.ConvertFrom(pContext, pCulture, pValue)
End Function
Public Overrides Function GetStandardValuesSupported(ByVal pContext As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValues(ByVal pContext As ITypeDescriptorContext) As StandardValuesCollection
Dim types As List(Of Type) = GetProjectTypes(pContext)
Dim values As List(Of String) = New List(Of String)()
For Each type As Type In types
values.Add(type.FullName)
Next
values.Sort()
Return New StandardValuesCollection(values)
End Function
Private Function GetProjectTypes(ByVal serviceProvider As IServiceProvider) As List(Of Type)
Dim typeDiscoverySvc = CType(serviceProvider.GetService(GetType(ITypeDiscoveryService)), ITypeDiscoveryService)
Dim types = typeDiscoverySvc.GetTypes(GetType(Object), True).Cast(Of Type)().Where(Function(item) item.IsPublic AndAlso GetType(Form).IsAssignableFrom(item) AndAlso Not item.FullName.StartsWith("System")).ToList()
Return types
End Function
Private Function GetTypeFromName(ByVal serviceProvider As IServiceProvider, ByVal typeName As String) As Type
Dim typeResolutionSvc As ITypeResolutionService = CType(serviceProvider.GetService(GetType(ITypeResolutionService)), ITypeResolutionService)
Return typeResolutionSvc.[GetType](typeName)
End Function
End Class