c# - Set assembly level attribute conditionally -
c# - Set assembly level attribute conditionally -
i set assembly level attribute conditionally based on value read config file. possible?
i read attributes static metadata , while info can changed @ run-time, changes have no applicability after application has started.
what other alternatives have?
update
my objective: trying do
i using traceattribute trace method entry , exit points. switch on or off attribute @ assembly level based on value config file. i'd have off times turn on in emergency situations collect evidence of problem environment.
i think more answered here: can attributes added dynamically in c#?
i'm still not sure why need bind assembly level attribute , config key, can pass config key attribute's constructor/property , resolve value within attribute logic. like:
[attributeusage(attributetargets.assembly)] public class traceattribute : attribute { public traceattribute(string traceconfigkey) { var keyvalue = configurationmanager.appsettings[traceconfigkey]; dotracing = !string.isnullorempty(keyvalue) && bool.parse(keyvalue); } /// <summary> /// utilize property tracing conditional logic. /// </summary> public bool dotracing { get; private set; } }
then in assemblyinfo:
[assembly: trace("dotracing")]
and config file:
<appsettings> <add key="dotracing" value="true"/> </appsettings>
as solution, if utilize existing attribute, , cannot customize it, can add together conditional compilation symbol project properties, write:
#if trace [assembly: trace()] #endif
but needs project recompilation, of course.
c# .net attributes assemblies
Comments
Post a Comment