如何使用类型字典为 class 创建可浏览的 属性
How to create a Browse-able property for class with type dictionary
我创建了一个 class 继承 win 表单中的按钮
我如何创建类型为 dictionary 的 Browsable 属性?
这是我的代码
但我没有在属性菜单中找到它
vb.net
<Browsable(True)>
Property Set_ddn() As Dictionary(Of TextBox, String)
Get
Return ddn
End Get
Set(ByVal value As Dictionary(Of TextBox, String))
ddn = value
End Set
End Property
如何让它可以浏览?
或者我应该用什么代替字典?
或其他解决方案
我在这里找到了答案 (C#)
https://whosebug.com/a/42829336/1543991
重写 vb.net
的代码
您应该通过从 CustomTypeDescriptor
派生或实现 ICustomTypeDescriptor
来编写自己的类型描述符。这是一个例子:
我的属性描述符
为 属性 提供自定义描述。在这里,我重写了属性 属性,为 属性 提供了一个新的属性列表。例如我检查 属性 是否有 [Category("Extra")]
,我还在它的属性 collection.
中添加了一个 [Browsable(false)]
Imports System
Imports System.ComponentModel
Imports System.Linq
Public Class MyPropertyDescriptor
Inherits PropertyDescriptor
Private o As PropertyDescriptor
Public Sub New(ByVal originalProperty As PropertyDescriptor)
MyBase.New(originalProperty)
o = originalProperty
End Sub
Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Return o.CanResetValue(component)
End Function
Public Overrides Function GetValue(ByVal component As Object) As Object
Return o.GetValue(component)
End Function
Public Overrides Sub ResetValue(ByVal component As Object)
o.ResetValue(component)
End Sub
Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
o.SetValue(component, value)
End Sub
Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Return o.ShouldSerializeValue(component)
End Function
Public Overrides ReadOnly Property Attributes As AttributeCollection
Get
Dim attributes = MyBase.Attributes.Cast(Of Attribute)().ToList()
Dim category = attributes.OfType(Of CategoryAttribute)().FirstOrDefault()
If category IsNot Nothing AndAlso category.Category = "Extra" Then attributes.Add(New BrowsableAttribute(False))
Return New AttributeCollection(attributes.ToArray())
End Get
End Property
Public Overrides ReadOnly Property ComponentType As Type
Get
Return o.ComponentType
End Get
End Property
Public Overrides ReadOnly Property IsReadOnly As Boolean
Get
Return o.IsReadOnly
End Get
End Property
Public Overrides ReadOnly Property PropertyType As Type
Get
Return o.PropertyType
End Get
End Property
End Class
MyTypeDescriptor
用于提供类型的自定义 属性 描述符列表。
Imports System
Imports System.ComponentModel
Imports System.Linq
Public Class MyTypeDescriptor
Inherits CustomTypeDescriptor
Private original As ICustomTypeDescriptor
Public Sub New(ByVal originalDescriptor As ICustomTypeDescriptor)
MyBase.New(originalDescriptor)
original = originalDescriptor
End Sub
Public Overrides Function GetProperties() As PropertyDescriptorCollection
Return Me.GetProperties(New Attribute() {})
End Function
Public Overrides Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection
Dim properties = MyBase.GetProperties(attributes).Cast(Of PropertyDescriptor)().[Select](Function(p) New MyPropertyDescriptor(p)).ToArray()
Return New PropertyDescriptorCollection(properties)
End Function
End Class
MyTypeDescriptionProvider
用于使用 TypeDescriptionProvider
属性将 MyTypeDescriptor
连接到 class。
Imports System
Imports System.ComponentModel
Public Class MyTypeDescriptionProvider
Inherits TypeDescriptionProvider
Public Sub New()
MyBase.New(TypeDescriptor.GetProvider(GetType(Object)))
End Sub
Public Overrides Function GetTypeDescriptor(ByVal type As Type, ByVal o As Object) As ICustomTypeDescriptor
Dim baseDescriptor As ICustomTypeDescriptor = MyBase.GetTypeDescriptor(type, o)
Return New MyTypeDescriptor(baseDescriptor)
End Function
End Class
MySampleClass
包含用 [Category("Extra")] 修饰的 属性。因此 Property2
在 属性 网格中将不可见。 (在 visual studio 或 collection 编辑器甚至 run-time 属性 网格中)
<TypeDescriptionProvider(GetType(MyTypeDescriptionProvider))>
Public Class MySampleClass
Public Property Property1 As Integer
<Category("Extra")>
Public Property Property2 As String
End Class
MyComplexComponent
包含 collection 个 MySampleClass
。所以你可以在 collection 编辑器中看到 MySampleClass
的行为。
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class MyComplexComponent
Inherits Component
Public Sub New()
MySampleClasses = New Collection(Of MySampleClass)()
End Sub
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property MySampleClasses As Collection(Of MySampleClass)
End Class
我创建了一个 class 继承 win 表单中的按钮 我如何创建类型为 dictionary 的 Browsable 属性?
这是我的代码 但我没有在属性菜单中找到它 vb.net
<Browsable(True)>
Property Set_ddn() As Dictionary(Of TextBox, String)
Get
Return ddn
End Get
Set(ByVal value As Dictionary(Of TextBox, String))
ddn = value
End Set
End Property
如何让它可以浏览? 或者我应该用什么代替字典?
或其他解决方案
我在这里找到了答案 (C#) https://whosebug.com/a/42829336/1543991
重写 vb.net
您应该通过从 CustomTypeDescriptor
派生或实现 ICustomTypeDescriptor
来编写自己的类型描述符。这是一个例子:
我的属性描述符
为 属性 提供自定义描述。在这里,我重写了属性 属性,为 属性 提供了一个新的属性列表。例如我检查 属性 是否有 [Category("Extra")]
,我还在它的属性 collection.
[Browsable(false)]
Imports System
Imports System.ComponentModel
Imports System.Linq
Public Class MyPropertyDescriptor
Inherits PropertyDescriptor
Private o As PropertyDescriptor
Public Sub New(ByVal originalProperty As PropertyDescriptor)
MyBase.New(originalProperty)
o = originalProperty
End Sub
Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Return o.CanResetValue(component)
End Function
Public Overrides Function GetValue(ByVal component As Object) As Object
Return o.GetValue(component)
End Function
Public Overrides Sub ResetValue(ByVal component As Object)
o.ResetValue(component)
End Sub
Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
o.SetValue(component, value)
End Sub
Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Return o.ShouldSerializeValue(component)
End Function
Public Overrides ReadOnly Property Attributes As AttributeCollection
Get
Dim attributes = MyBase.Attributes.Cast(Of Attribute)().ToList()
Dim category = attributes.OfType(Of CategoryAttribute)().FirstOrDefault()
If category IsNot Nothing AndAlso category.Category = "Extra" Then attributes.Add(New BrowsableAttribute(False))
Return New AttributeCollection(attributes.ToArray())
End Get
End Property
Public Overrides ReadOnly Property ComponentType As Type
Get
Return o.ComponentType
End Get
End Property
Public Overrides ReadOnly Property IsReadOnly As Boolean
Get
Return o.IsReadOnly
End Get
End Property
Public Overrides ReadOnly Property PropertyType As Type
Get
Return o.PropertyType
End Get
End Property
End Class
MyTypeDescriptor
用于提供类型的自定义 属性 描述符列表。
Imports System
Imports System.ComponentModel
Imports System.Linq
Public Class MyTypeDescriptor
Inherits CustomTypeDescriptor
Private original As ICustomTypeDescriptor
Public Sub New(ByVal originalDescriptor As ICustomTypeDescriptor)
MyBase.New(originalDescriptor)
original = originalDescriptor
End Sub
Public Overrides Function GetProperties() As PropertyDescriptorCollection
Return Me.GetProperties(New Attribute() {})
End Function
Public Overrides Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection
Dim properties = MyBase.GetProperties(attributes).Cast(Of PropertyDescriptor)().[Select](Function(p) New MyPropertyDescriptor(p)).ToArray()
Return New PropertyDescriptorCollection(properties)
End Function
End Class
MyTypeDescriptionProvider
用于使用 TypeDescriptionProvider
属性将 MyTypeDescriptor
连接到 class。
Imports System
Imports System.ComponentModel
Public Class MyTypeDescriptionProvider
Inherits TypeDescriptionProvider
Public Sub New()
MyBase.New(TypeDescriptor.GetProvider(GetType(Object)))
End Sub
Public Overrides Function GetTypeDescriptor(ByVal type As Type, ByVal o As Object) As ICustomTypeDescriptor
Dim baseDescriptor As ICustomTypeDescriptor = MyBase.GetTypeDescriptor(type, o)
Return New MyTypeDescriptor(baseDescriptor)
End Function
End Class
MySampleClass
包含用 [Category("Extra")] 修饰的 属性。因此 Property2
在 属性 网格中将不可见。 (在 visual studio 或 collection 编辑器甚至 run-time 属性 网格中)
<TypeDescriptionProvider(GetType(MyTypeDescriptionProvider))>
Public Class MySampleClass
Public Property Property1 As Integer
<Category("Extra")>
Public Property Property2 As String
End Class
MyComplexComponent
包含 collection 个 MySampleClass
。所以你可以在 collection 编辑器中看到 MySampleClass
的行为。
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class MyComplexComponent
Inherits Component
Public Sub New()
MySampleClasses = New Collection(Of MySampleClass)()
End Sub
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property MySampleClasses As Collection(Of MySampleClass)
End Class