注入 TypeConverter 对于框架类型安全吗?
这明智吗?
class MyTypeConverter : TypeConverter {
// implementation
}
// elsewhere
TypeDescriptor.AddAttributes(typeof(string[]),
new[] { new TypeConverterAttribute(typeof(MyTypeConverter)) });
请注意,我将其放在 string[]
上。
这样做感觉很脏。
Is this wise?
class MyTypeConverter : TypeConverter {
// implementation
}
// elsewhere
TypeDescriptor.AddAttributes(typeof(string[]),
new[] { new TypeConverterAttribute(typeof(MyTypeConverter)) });
Note I'm putting this on string[]
.
It feels dirty to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安全的?是的,Windows 窗体设计器使用它来注入属性,因此它是一个有效的功能,尽管使用率并不高。
肮脏的?是的,有一点。如果有其他方法可以给猫剥皮,我会考虑这样做。属性是简单的元数据,用于提供一种干净的方式来指定代码的某些特征。通过使用动态属性,您就脱离了正常的用例。
Safe? Yes, it's used by the Windows Forms Designer to inject attributes so it's a valid piece of functionality, although not heavily used.
Dirty? Yeah, a little bit. If there's some other way of skinning the cat, I'd look at doing it that way. Attributes are meant to be simple metadata used to provide a clean way to specify certain characteristics of your code. By using dynamic attributes, you're kind of going out of the normal use case.
如果你需要这样做,那么你就需要这样做。不过,请确保您实际上不仅仅需要包含类的只读属性。
不过,我建议使用 typeof (IEnumerable),这样您也可以选择列表、集合等。
If you need to do it, then you need to do it. Be sure you don't really just need a readonly property on a containing class, though.
However, I would suggest using typeof (IEnumerable), so you can pick up lists, collections, etc., as well.