如何在 PropertyInspector 中的抽象类型属性上使用 ExpandableObjectConverter
我想让 PropertyInspector
允许我选择要在抽象类型的属性中设置的实例的具体类型,并使用通常提供的 [+] 符号扩展到该类型的属性通过ExpandableObjectConverter
。
然而,当使用抽象类型作为属性时,列表中唯一可选的值似乎是 (null)
,并且没有任何内容被扩展。我可以理解这一点,因为编辑器无法知道我想提供哪些类型。
但是,如果我使用自己的编辑器提供 EditorAttribute,将派生自抽象类型的具体实例分配给属性,则不会出现 [+] 符号,并且我无法扩展到子属性。
我使用了一些简单的测试类,如下所示:
public abstract class Test
{
public string Name { get; set; }
}
public class TestImpl : Test
{
public string Description { get; set; }
}
public class MyObject
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public Test MyTest { get; set; }
}
我怎样才能实现我想要的,即让 MyTest 属性有一个编辑器,允许我选择我想要的具体类型,当我这样做时,会出现一个 [+] 符号应该允许我设置该对象的属性?
编辑:
在Windows窗体中使用普通的PropertyGrid时,以下转换器几乎可以完全满足我的要求:
public class MyConverter : ExpandableObjectConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
StandardValuesCollection coll = new StandardValuesCollection(new Test[] { new TestImpl() });
return coll;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return Activator.CreateInstance(Type.GetType((string)value));
}
}
但是在工作流设计器(或PropertyInspector)中,它似乎根本没有添加任何功能(或者至少不是我想要的)。事实上,转换器上似乎没有任何方法被调用,这看起来很奇怪。
将属性 MyObject.MyTest
的类型更改为 TestImpl
,ExpandableObjectConverter
可以正常工作。
(实际上,当然会有多个类派生自抽象 Test
)
I want to have the PropertyInspector
allow me to select the concrete type of the instance to set in a property of an abstract type and expand into the properties of that type using the [+] sign as is normally provided by the ExpandableObjectConverter
.
However it seems that when using an abstract type as the property, the only selectable value in the list is (null)
, and nothing gets expanded. This I can understand since there is no way for the editor to know which types I want to provide.
But if I provide by own editor using the EditorAttribute that assigns to the property a concrete instance derived from the abstract type, no [+]-sign appears and I cannot expand into the sub-properties.
I was using some simple test classes like the following:
public abstract class Test
{
public string Name { get; set; }
}
public class TestImpl : Test
{
public string Description { get; set; }
}
public class MyObject
{
[TypeConverter(typeof(ExpandableObjectConverter))]
public Test MyTest { get; set; }
}
How can I achieve what I want, i.e. to have the MyTest property have an editor allowing me to select the concrete type I want, and when I have done so, a [+] sign should appear to allow me to set the properties of that object?
Edit:
When using a normal PropertyGrid in Windows Forms the following converter would do pretty much exactly what I want:
public class MyConverter : ExpandableObjectConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
StandardValuesCollection coll = new StandardValuesCollection(new Test[] { new TestImpl() });
return coll;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return Activator.CreateInstance(Type.GetType((string)value));
}
}
However in the workflow designer (or PropertyInspector) it seems to add no functionality at all (or at least not the one I want). In fact it seems that no methods on the converter are even called, which seems very strange.
Changing the type of the property MyObject.MyTest
to TestImpl
, the ExpandableObjectConverter
does work.
(In reality of course there would be more than one class deriving from the abstract Test
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论