Let say I got the following :
public class A {
[MyAttribute]
public void Test() {}
}
I don’t like the behavior of adding an attribute to prefix a method or a class with []. I think that make the code less readable.
I find that a better approach can be something like :
AddAttribute(A, Test, MyAttribute);
Is there a way of doing something like that ?
Because that is some kind of adding attribute at runtime…
Or is there other way to use attribute ? Like xml files ?
Attributes cannot be added at runtime, they are embedded in the metadata of the assembly. You can, however, add attributes by using an external tool during build that modifies your source code before compiling.