Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use a global setting as an attribute argument

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

like image 904
Jowen Avatar asked Jan 24 '23 22:01

Jowen


1 Answers

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

like image 194
Jon Skeet Avatar answered Feb 04 '23 22:02

Jon Skeet