-2

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,
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
user646265
  • 1,011
  • 3
  • 14
  • 32
  • 2
    Did you try it? In the time that it took you to post this in StackOverflow, the compiler would have given you the error! – RQDQ Nov 30 '11 at 17:42
  • This question could be answered when trying to compile the code itself. – Felix K. Nov 30 '11 at 17:46
  • Yes I did try it, and I saw that it didn't work, but I thought that I may be going about it the wrong way; now, as I suspected, it has been shown not to work. – user646265 Nov 30 '11 at 17:55

3 Answers3

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.
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
2

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.

Christian Klauser
  • 4,416
  • 3
  • 31
  • 42
1

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445