I have a question:
Is there an elegant way of getting Attributes on a referenced field.
Ie.:
public class C1: Base
{
[MyAttribute]
public string Field1;
}
public class Base
{
private void Do(ref string field)
{
if (field has attributes)
DoSomething();
}
}
How can I get attributes of a field in the method Do()?
Thanks in advance.
There’s no way you can do that with
ref string fieldsignature. Attributes are applied to declarations (fields, classes, events etc.), not to “instances”.What you can do, is alter your method like this:
and then use reflection to inspect
fieldContainingTypeto see, what attributes are applied to field namedfieldName. This approach, however, is extremely fragile and generally very bad.