5

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

1

1 Answer 1

11

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).

Sign up to request clarification or add additional context in comments.

4 Comments

still it behaves as same? can't bind runtime values?
@ManirajSS: Attributes are compiled into the assembly, so no, they can't reflect anything at execution time. That doesn't stop whatever uses them from doing things at execution time of course - but that's only helpful if it's your code that is using them...
Thanks. So any suggestions/alternate options to bind values for cache expiry dynamic params of cache attribute?
@ManirajSS: Not in the comments of a question from over 15 years ago, for sure. Please ask a new question with appropriate details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.