I have some logic which is like this:
#define MYVAR
...
#if MYVAR
[Attribute1]
#else
[Attribute2]
#endif
I want to be able to switch the attributes applied to a method based on a environment variable
Like if the environment variable MYVAR
is set, then actually define the symbol 'MYVAR' and vice-versa.
TBH I do not know if it is possible. If not any ideas how to overcome this? I can ofcourse add this in the method itself - the env. variable check, but I need the attributes because of reflection being executed.
I can create a custom MyAttrbute(int mode)
but I cannot add a logic in its constructor to extract an env. variable, because it must be a constant expression.
Possible solutions:
Like the first comment suggests, I can read the env. variable in the custom attribute. Still there will be some nasty reflection left to do.
Because I am working on Linux with dotnet commands, I can actually bundle my build command with pre-step
sed
command and add a dummy placeholder{{define_myvar}}
to the top of the.cs
file and replace it with either empty string or the full string based on env. variables. No reflection here, but not very elegant.