HOWTO?: enumerating custom attributes

D

davearkley

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.GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);

foreach (FieldInfo Field in Fields)
{
if (!Attribute.IsDefined(Field, typeof(MyAttribute)))
{
Console.WriteLine(Field.Name)
}
else
{
MyAttribute TheAttribute =
(MyAttribute)Field.GetCustomAttributes(typeof(Field), false)[0];

if (0 == MyAttribute.Name.Length) Console.WriteLine(Field.Name);
else Console.WriteLine(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
'.GetCustomAttributes" where I don't already know the type.


Can anyone help please?
 
J

Jon Shemitz

Field.GetCustomAttributes(typeof(Field), false)[0];
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
'.GetCustomAttributes" where I don't already know the type.

This is a strange question. If you found GetCustomAttributes, surely
you also saw the overload that doesn't take a Type parameter?

GetCustomAttributes(bool) returns all the attributes, regardless of
type. The bool parameter specifies whether the array should include
inherited attributes or not.
 
D

davearkley

Jon said:
Field.GetCustomAttributes(typeof(Field), false)[0];
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
'.GetCustomAttributes" where I don't already know the type.

This is a strange question. If you found GetCustomAttributes, surely
you also saw the overload that doesn't take a Type parameter?

GetCustomAttributes(bool) returns all the attributes, regardless of
type. The bool parameter specifies whether the array should include
inherited attributes or not.

--

Jon,

You're right - that overload is right there in the docs. I must have
stared at that page over half a dozen times and didn't see it. Thanks
for helping me see the obvious.
 
D

Dave Sexton

Hi,
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.

<snip>

There are a few issues with your code, some of which will prevent it from
compiling. Try this instead:

public static void DisplayObsoleteFields(Type type)
{
FieldInfo[] fields = type.GetFields(
BindingFlags.Instance | BindingFlags.NonPublic);

Type attributeType = typeof(ObsoleteAttribute); // for testing

foreach (FieldInfo field in fields)
{
Attribute[] attributes =
Attribute.GetCustomAttributes(field, attributeType, false);

if (attributes.Length == 0)
Console.WriteLine("{0}: MyAttribute not defined", field);
else
{
foreach (ObsoleteAttribute attribute in attributes)
{
Console.WriteLine("{0}: MyAttribute(Message={1})",
field, attribute.Message);
}
}
}
}


On a side note, I recommend that you use lower camel case for local variable
names so that it's obvious you're not using static properties and methods
and to prevent conflicts with classes of the same name.
 
D

Dave Sexton

Hi,

In my example, I forgot to change "MyAttribute" to "Obsolete" in a couple of
string literals, sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top