注入 TypeConverter 对于框架类型安全吗?

发布于 2024-12-16 12:37:06 字数 288 浏览 1 评论 0原文

这明智吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-23 12:37:06

安全的?是的,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.

桃气十足 2024-12-23 12:37:06

如果你需要这样做,那么你就需要这样做。不过,请确保您实际上不仅仅需要包含类的只读属性。

不过,我建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文