Dynamic parameters for attributes

允我心安 提交于 2019-12-08 11:06:23

问题


I know parameters to attribute declarations have to be a constant expression and resolved at compile time. However, can I play with the concept of 'compile time'? ASP.net has the concept of App_Code folder. It looks from it's description like you can drop .cs files into it, even while the app is running, and it will be brought in and compiled. Is it possible to use this mechanism to dynamically create an Enum or const array that can be used in an attribute declaration?

[SomeAttribute(ValidTypes.SomeType)]
public class Foo
{
}

Basically, I want to dynamically grow the valid types in ValidTypes without having to recompile all components that reference it. And I need to keep it in sync with a list of values in a database table. So, my question is, can I drop a .cs file with the definition for an Enum or const string array into App_Code and have it automagically show up? Or better yet, is the mechanism .Net uses to do this available to be called elsewhere so I don't have to do it in an ASP.Net app?


回答1:


even if you could, you'd have to recompile the app to use the added values. sounds like what you want is the code in the attribute to look up the dynamic values.



来源:https://stackoverflow.com/questions/3814866/dynamic-parameters-for-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!