在不修改类的情况下在属性网格中扩展自定义对象? [英] Expand custom object in property grid without any modifications to the class?
问题描述
默认情况下,自定义对象是灰色的,并且不可扩展:
By default, custom objects are grayed out, and not expandable:
我知道可以使用 ExpandableObjectConverter 使它们可扩展,但这需要扩展原始类.
I know it's possible to make them expandable using ExpandableObjectConverter, but that requires extending the original class.
如果不能修改原来的类怎么办?是否有处理属性网格内自定义对象扩展的通用方法?
What if I cannot modify the original class? Is there a generic approach of handling custom object expansion inside a property grid?
推荐答案
您可以在运行时为 ExpandableObjectConverter
动态添加 TypeConverterAttribute
,使用类似以下内容(VB.NET).
You can dynamically add the TypeConverterAttribute
for the ExpandableObjectConverter
at runtime, using something like the following (VB.NET).
Dim attr = New TypeConverterAttribute(GetType(ExpandableObjectConverter))
TypeDescriptor.AddAttributes(GetType(ExtensionDataObject), attr)
您需要为您想要扩展的每种类型AddAttribute
.如果类型都在特定的命名空间中,您可以使用反射来查找它们:
You would need to AddAttribute
for each type you want to be expandable. If the types are all in a specific namespace, you could use reflection to find them:
Dim assm = Assembly.GetExecutingAssembly() ' or some other assembly
For Each t In assm.GetTypes().Where(Function(x) x.Namespace = "InterestingTypes")
TypeDescriptor.AddAttributes(t, attr)
Next
(抱歉语法高亮显示损坏 - 我应该使用 C#!)
(sorry for the broken syntax highlighting - I should have used C#!)
这篇关于在不修改类的情况下在属性网格中扩展自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!