[MyAttribute(Name = "Test123"]
public MyClass
{
public string Name
{
get { return "Test123"; }
}
}
Is this code sample, I need to define the name two times. One time in the attribute and one time in the class itself. Is there a way to avoid this kind of duplication?
I need both because sometime, I’m iterating through the assembly to find all class that use MyAttribute and to list them (with the name). Other time, I’ve access to the instantiated object so I don’t want to use the attribute.
If the name can be put into a string constant, then you can use that constant for both the attribute parameter and also the return value of the property.
If you ever need to change the name, just change the const value.