TypeDescriptor.CreateProperty 不添加属性
我正在尝试使用 TypeDescriptor.CreateProperty 将属性添加到类型,以便在属性网格中显示附加属性,但是未添加此新属性,当我对该类型调用 TypeDescriptor.GetProperties 来检查属性时,此属性不存在。
我可能会遗漏或忽略一些东西吗? 据我所知,这是一个基本且简单的场景。
这是电话: TypeDescriptor.CreateProperty(typeof (MovieMenuItem), "ExternalMediaLocation", typeof (string), null);
I am trying to add a property to a type using TypeDescriptor.CreateProperty in order to display an additional property in a property grid, however this new property is not added and when I call TypeDescriptor.GetProperties on that type to inspect the properties, this property does not exist.
It there something I might be missing or overlooking? This is a basic and simple scenario as far as I remember.
Here is the call:
TypeDescriptor.CreateProperty(typeof (MovieMenuItem), "ExternalMediaLocation", typeof (string), null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CreateProperty
只是为您返回相关类型的基于反射的PropertyDescriptor
(例如,它不是AddProperty
)。 这里的场景是怎样的? 如果您只需要在 DataGridView 中显示额外的数据,最简单的选择就是向网格添加额外的未绑定列。您可以在运行时扩展类型,但对于列表,您有两个主要选项:
ITypedList
(如果列表的每个实例可以有不同的列) - 请参阅这个答案TypeDescriptionProvider
- 允许您为每种类型添加自定义属性(最终归结为编写一个PropertyDescriptor
,就像第一个示例 - 但不同的挂钩)如何获取基于列表的元数据的完整规则位于 这个答案
CreateProperty
just gives you back a reflection-basedPropertyDescriptor
for the type in question (it isn'tAddProperty
, for example). What is the scenario here? If you just need to display extra data inDataGridView
, the simplest option is simply to add an extra unbound column to the grid.You can extend types at runtime, but for lists you have two main options:
ITypedList
(if each instance of the list can have different columns) - see this answerTypeDescriptionProvider
- allows you to add custom properties per-type (ultimately boils down to writing aPropertyDescriptor
, just like the first example - but different hooks)The full rules of how list-based metadata is fetched are in this answer