4

I want to specify an argument in an attribute, like this:

[OutputCache(Duration = GlobalSettings.GlobalVar)]

Where GlobalVar is a variable I defined only once (don't care where).

Using a configuration setting doesn't work anyhow, and I can't get it working with some static class either.

I get the error: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Maybe it's possible to write a custom wrapper around OutputCache, but that feels like a hack. Am I missing something here??

Jowen
  • 4,975
  • 1
  • 42
  • 40

1 Answers1

9

The problem is that attribute values have to be determined at compile time - they're embedded in the assembly. That's why you have to use a constant expression (not just a global variable - truly a constant).

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049