Skip to content

Part Creation Policy Not Export

"ExportAttribute" and "PartCreationPolicyAttribute" should be used together.

What does this mean ?

The Managed Extensibility Framework (MEF) PartCreationPolicyAttribute property is used to describe how the exported object will be constructed. As a result, utilizing the ExportAttribute property to export this class with this attribute makes no sense.

What can happen ?

When a class is declared as shared with a PartCreationPolicyAttribute but not an ExportAttribute, this rule causes a problem.

Recommendation

ExportAttribute should be combined with PartCreationPolicyAttribute.

Sample Code

Vulnerable :

[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant
public class FooBar : IFooBar
{
}

Non Vulnerable :

[Export(typeof(IFooBar))]
[PartCreationPolicy(CreationPolicy.Any)]
public class FooBar : IFooBar
{
}

References