C # attributes \ avoid hardcode values

Just to confirm that there is no way to avoid hard-coded values ​​in C # attributes?

[SomeAttribute(3+1)] public void Foo(string s) 

or members of an access class or to not precompile anything?

Now I will look at a great example of postharp's retry mechanism - and would like to know if I can configure the number of retries from outside the system

+4
source share
1 answer

The arguments of the attribute constructor and property values ​​are baked into compiled code. They cannot be determined at runtime.

Of course, if you have an attribute that is ready to play the ball, you can give it (say) the type and name of the property and ask it to get that property value at run time. (This is what NUnit does for [TestCaseSource] , for example.) But you cannot do this with an attribute that does not know this.

+5
source

Source: https://habr.com/ru/post/1447178/


All Articles