I've recently 'discover' the wonders of custom attributes and
reflection. There's one aspect that has stumping me and I've been
unable to find samples in the docs or on the web.
I have fields in a class which have zero, one or more custom attributes
associated with each field. I'd like to get a list of the attributes
present for a given field, and then iterate over them to find
associated values.
Where I've coded for the presence of one attribute I've used:
FieldInfo[] Fields = TypeData.GetFie lds(BindingFlag s.Instance |
BindingFlags.No nPublic);
foreach (FieldInfo Field in Fields)
{
if (!Attribute.IsD efined(Field, typeof(MyAttrib ute)))
{
Console.WriteLi ne(Field.Name)
}
else
{
MyAttribute TheAttribute =
(MyAttribute)Fi eld.GetCustomAt tributes(typeof (Field), false)[0];
if (0 == MyAttribute.Nam e.Length) Console.WriteLi ne(Field.Name);
else Console.WriteLi ne(MyAttribute. Name);
}
}
I can see how this works - I'm telling it the type of attribute
(MyAttribute) to look for. What I cant find the syntax for is to
'.GetCustomAttr ibutes" where I don't already know the type.
Can anyone help please?