问题描述
我试图用一个ExpandoObject作为一个PropertyGrid中的SelectedObject。我知道如何我想要的属性添加到ExpandoObject:
I'm trying to use an ExpandoObject as the SelectedObject of a PropertyGrid. I know how to add the properties I want to the ExpandoObject:
public dynamic MakePropertyObject()
{
dynamic expando = new ExpandoObject();
var dictionary = expando as IDictionary<string, object>;
foreach(MyClass m in PropertiesINeedToAdd)
dictionary[m.Name] = m.Value;
return expando;
}
这code的精细调试工作显示了的expando
的属性的名称和值如预期。
This code's working fine- the debugger shows the names and values of expando
's properties as expected.
然而,没有生成的属性是显示了PropertyGrid中当我设置 MakePropertyObject()
到 SelectedObject 返回值code>属性。我认为(也许错误地),这是因为
或 ExpandoObject
的属性没有任何 DisplayNameAttribute
,<$任何C的用来控制属性显示在其他属性$ C> DescriptionAttribute 的PropertyGrid
。
However, none of the generated properties is showing up in the PropertyGrid when I set the return value of MakePropertyObject()
to its SelectedObject
property. I assume (perhaps falsely) that this is because the ExpandoObject
's properties don't have any DisplayNameAttribute
, DescriptionAttribute
, or any of the other attributes used to control how properties are displayed in a PropertyGrid
.
我已经做了一些阅读和一些谷歌搜索,我想不通,如果有一种方法用自定义属性装饰一个 ExpandoObject
的生成的属性。有谁知道如何可以做到这一点,或者更好的方式来显示一个 ExpandoObject
在的PropertyGrid
?
I've done some reading and some Googling, and I can't figure out if there's a way to decorate the generated properties of an ExpandoObject
with custom attributes. Does anyone know how this can be done, or of a better way to show an ExpandoObject
in a PropertyGrid
?
解决方法:
由@Stephen克利里提供的答案是正确的,乐于助人(感谢,斯蒂芬)。对于其他有同样问题,实施 ICustomTypeDescriptor
工作完美的我。
The answer provided by @Stephen Cleary was correct and helpful (thanks, Stephen). For others with the same problem, implementing ICustomTypeDescriptor
worked perfectly for me.
作为一个侧面说明, 实现 ICustomTypeDescriptor
提供的属性和事件描述符本身,而不是另一个对象的对象的。我想描述符和描述都应该通过在一线似乎混乱和多余的,我认为一个对象应该描述自己的类型的属性什么的联系在一起,但是这确实是多么的PropertyGrid
s使用 ICustomTypeDescriptor
接口。
As a side note, the object that implements ICustomTypeDescriptor
provides the property and event descriptors for itself, not for another object. I thought the descriptor and the described were supposed to be linked by an attribute or something at first- it seemed confusing and redundant to me that an object should describe its own type, but that's indeed how PropertyGrid
s use the ICustomTypeDescriptor
interface.
推荐答案
这个问题实际上是为预期的动态类型的反射不起作用。
The problem is actually that reflection doesn't work as expected on dynamic types.
的PropertyGrid
使用反射来检查其对象的属性,而 ExpandoObject
没有任何(静态)属性。
PropertyGrid
uses reflection to examine its object's properties, and ExpandoObject
doesn't have any (static) properties.
您可以实现 ICustomTypeDescriptor
劫持的反思和查询 ExpandoObject
的(动态)的属性。中的将是一个很好的起点。
You can implement ICustomTypeDescriptor
to "hijack" the reflection and query the (dynamic) properties of the ExpandoObject
. The code for DynamicTypeDescriptorWrapper
in this blog post would be a good starting point.
这篇关于是否有可能的属性添加到ExpandoObject实例产生的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!