Skip to content

Part Creation Policy Not Export

"PartCreationPolicyAttribute" should be used with "ExportAttribute"

What does this mean ?

The PartCreationPolicyAttribute attribute, which is part of the Managed Extensibility Framework (MEF), is used to specify how the exported object will be created. Therefore it doesn't make sense not to export this a class with this attribute using the ExportAttribute attribute.

What can happen ?

This rule raises an issue when a class is marked as shared with a PartCreationPolicyAttribute but lacks a ExportAttribute.

Recommendation

PartCreationPolicyAttribute should be used with ExportAttribute.

Sample Code

Vulnerable :

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

Non Vulnerable :

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

References :