views:

1198

answers:

2

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??

+5  A: 

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
A: 

decimal.Zero is a constant but still not working for me. Can somebody help?

David
When I check the Decimal.Zero in Reflector, it returns: *public static readonly decimal Zero* That's (probably) not a true constant...
Jo-wen