问题
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