C# - 这可能吗? - [DescriptionAttribute()] 带有文本框文本

发布于 2024-12-18 23:34:01 字数 238 浏览 2 评论 0原文

枚举中的 DescriptionAttribute 可以包含 TextBox 的文本吗?我问这个问题是因为我有一个包含大量 TextBox 的文件,我希望将它们的内容与我拥有的值相匹配。我怀疑我能做到这一点,但我一点也不确定。

IE

[DescriptionAttribute(textBox1.Text)]
a,

Can a DescriptionAttribute in an enumeration contain a TextBox'es text? I ask because I have a file with numerous TextBoxes and I was hoping to match the content of them with a value that I have. I doubt I can do this, but I'm not sure at all.

i.e.

[DescriptionAttribute(textBox1.Text)]
a,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南…巷孤猫 2024-12-25 23:34:01

不,属性需要编译时常量作为参数。

C# 规范说:

如果以下所有陈述都为真,则表达式 E 是属性参数表达式:

  • E 的类型是属性参数类型(第 24.1.3 节)。
  • 在编译时,E 的值可以解析为以下之一:
    • 一个常数值。
    • 指定非泛型类型、封闭构造类型(第 25.5.2 节)的 typeof 表达式(第 14.5.11 节)或
      未绑定的泛型类型(第 25.5 节)。
    • 属性参数表达式的一维数组。

No, attributes need compile time constants as parameters.

The C# specification says:

An expression E is an attribute-argument-expression if all of the following statements are true:

  • The type of E is an attribute parameter type (§24.1.3).
  • At compile-time, the value of E can be resolved to one of the following:
    • A constant value.
    • A typeof-expression (§14.5.11) specifying a non-generic type, a closed constructed type (§25.5.2), or
      an unbound generic type (§25.5).
    • A one-dimensional array of attribute-argument-expressions.
昨迟人 2024-12-25 23:34:01

不,这是不可能的,因为属性是程序集元数据的一部分,即嵌入在您正在编译的 .dll/.exe 中。

您不能在编译时引用仅在运行时存在的值。

如果您想要类似这样的东西,您必须自己构建它,即创建一个类,在运行时将枚​​举的字段映射到文本框的值。

No that is no possible, since attributes are part of the assmebly's meta data, i.e. embedded in the .dll/.exe you're compiling.

You cannot, at compile time, refer to a value that will only exist at runtime.

If you want something even remotely like this, you'll have to build it for yourself, i.e., create a class that maps fields of an Enum to the values of textboxes at runtime.

蓝色星空 2024-12-25 23:34:01

不可以。属性是在编译时定义的,并且必须是常量值。

No. Attributes are defined at compile time and have to be constant values.

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