我使用this文章实现Custom Attributes
为enum
,一切都很好用hard coding
值,但我需要传递的参数run time
,例如:带有自定义属性和const扩展枚举值问题
enum MyItems{
[CustomEnumAttribute("Products", "en-US", Config.Products)]
Products
}
的Config.Products (bool value)
的问题,错误的是:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
有没有什么办法来解决日是什么?
更新
的enum
(在这种情况下MyItems)有20个项目,每个项目必须有custom attribute
,然后我想生成从枚举的项目菜单,取决于Culture
我得到匹配的标题,也取决于Config
,我决定从菜单中显示/隐藏项目(事实上,如果Config.X == false,我不会将项目添加到菜单中)
另外,对于Config,我有另一个系统,我想与菜单同步该系统,这就是我想要获得的原因210在运行时。
谢谢!
相反的属性,你可以创建一个扩展方法,找到'配置。' –
Vlad274
对不起,但没有得到你?我知道如何创建扩展方法,但我怎么能混合这2件事? –
你可以访问'配置。产品'属性构造函数或静态方法'Get'? – Vova