kanayabhattad / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

When creating a metadata attribute that inherits from another metadata attribute, it's necessary to re-apply the [MetadataAttribute] attribute #442

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In Autofac.Extras.Attributed, the MetadataHelper class is used to lookup 
associated metadata with a component. The way this is done is in line 47 like 
so:

            foreach (var attribute in targetType.GetCustomAttributes(true)
                                                .Where(p => p.GetType().GetCustomAttributes(typeof(MetadataAttributeAttribute), false).Any()))
                propertyList.AddRange(GetProperties(attribute));

Note how the GetCustomAttributes (the second one, the one that is trying to 
determine if the attribute is a metadata attribute) is explicitly passing 
"false" to the "inherit" value for the lookup. But the 
MetadataAttributeAttribute has an AttributeUsage that specifies that it IS 
inherited, so the expectation from a developer would be that the metadata 
attribute lookup indeed looks up the inheritance chain for this attribute. 
Meaning that a derived metadata attribute does not need to specify again 
[MetadataAttribute].

Maybe the proper way to do this just in case it changes in the future, is to 
grab the AttributeUsageAttribute from the MetadataAttributeAttribute class and 
use the Inherits property to pass to the GetCustomAttributes call, like so:

var attributeUsage = typeof(MetadataAttributeAttribute)
    .GetCustomAttributes(typeof(AttributeUsageAttribute), false)
    .OfType<AttributeUsageAttribute>()
    .First();

foreach (var attribute in targetType.GetCustomAttributes(true)
                                    .Where(p => p.GetType().GetCustomAttributes(typeof(MetadataAttributeAttribute), attributeUsage.Inherited).Any()))
    propertyList.AddRange(GetProperties(attribute));

Or maybe that's overkill and it should just pass true ;).

Original issue reported on code.google.com by dan...@cazzulino.com on 7 Jun 2013 at 2:10

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 7 Jun 2013 at 6:11

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 0503fd8df079.

Original comment by travis.illig on 7 Jun 2013 at 6:19

GoogleCodeExporter commented 8 years ago
I went ahead and just passed true since the GetCustomAttributes call will obey 
the attribute usage if it changes.

Original comment by travis.illig on 7 Jun 2013 at 6:20