使用全局设置作为一个属性参数 [英] use a global setting as an attribute argument
问题描述
我想在一个属性来指定一个参数,像这样:
I want to specify an argument in an attribute, like this:
[OutputCache(Duration = GlobalSettings.GlobalVar)]
在哪里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.
我得到的错误:
的属性参数必须是一个常量前pression,属性参数类型的typeof前pression或数组创建前pression 的
也许这是可以写身边的OutputCache一个定制的包装,但感觉就像一个黑客攻击。我失去了一些东西在这里?
Maybe it's possible to write a custom wrapper around OutputCache, but that feels like a hack. Am I missing something here??
推荐答案
的问题是,属性值在编译时确定 - 他们嵌入汇编。这就是为什么你必须使用一个恒定的前pression。(不只是一个全局变量 - 一个真正的恒的)
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).
这篇关于使用全局设置作为一个属性参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!