我在这里找到了答案(C#)
https://stackoverflow.com/a/42829336/1543991
重写代码vb.net
您应该通过派生CustomTypeDescriptor
或实现来编写自己的类型描述符ICustomTypeDescriptor
。这是一个例子:
我的属性描述符
为属性提供自定义描述。在这里,我覆盖 Attributes 属性以提供属性的新属性列表。例如,我检查属性是否有[Category("Extra")]
,我还在[Browsable(false)]
其属性集合中添加了 a 。
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
我的类型描述符
用于提供类型的自定义属性描述符列表。
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
用于使用属性连接MyTypeDescriptor
到类。TypeDescriptionProvider
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
在属性网格中将不可见。(在视觉工作室或集合编辑器甚至运行时属性网格中)
<TypeDescriptionProvider(GetType(MyTypeDescriptionProvider))>
Public Class MySampleClass
Public Property Property1 As Integer
<Category("Extra")>
Public Property Property2 As String
End Class
我的复杂组件
包含MySampleClass
. 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