Change of inherited attribute behaviour with ConventionBuilder

Jul 2, 2012 at 9:05 PM

I've noticed a change in behaviour of attribute based exports with the MEF lightweight composition framework when simply adding a ConventionBuilder (without any further setup). My expectation was that adding a ConventionBuilder shouldn't alter the behaviour of any existing attribute based exports. Is this correct?

The particular case I am having issues with is when using a derived ExportAttribute where the [AttributeUsage(..., Inherited=true)]. If I add some parts to a container without using the ConventionBuilder then only the types with the custom export attribute as exported, however if I supply a ConventionBuilder then all derived types are exported also. Whilst the latter is maybe the expected behaviour, I didn't expect specifying a ConventionBuilder to break existing exports.

A test case to show this behaviour is shown below,

[CustomExport]
public class Base
{
}

public class Derived : Base
{
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
[MetadataAttribute]
public class CustomExportAttribute : ExportAttribute
{
    public CustomExportAttribute()
        : base(typeof(Base))
    {
    }
}

[TestMethod]
public void InheritedCustomAttributeBehaviourIsDifferentWithConventionBuilder()
{
    var cb = new ConventionBuilder();
    var c = new ContainerConfiguration()
        .WithParts(new[] { typeof(Base), typeof(Derived) }, cb)
        .CreateContainer();

    c.GetExport<Base>();
}

This fails with a CompositionFailedException stating that more than one export for "Base" can be found (i.e. "Base" and "Derived"). If however the ConventionBuilder is removed from the AddParts call then this test passes (only "Base" is exported).

The solution is to set "Inherited = false" and the test again passes with or without a ConventionBuilder. Note that this may be an issue as Inherited is true by default and so if not specified then the change in behaviour occurs.

Regards,

    Andy

Jul 3, 2012 at 9:52 PM

Andy, thanks for pointing this out, there is definitely an inconsistency here that we'll investigate.

Regards,

Nick

Jul 17, 2012 at 10:29 PM

Hi Andy,

Just to close the loop here, we plan to bring the ConventionBuilder behavior back in line with the default container here.

Regards,
Nick

Jul 18, 2012 at 11:26 AM

Hi Nick,

Thanks for the update. That sounds good to me.

Andy