S4428 — "PartCreationPolicyAttribute" should be used with "ExportAttribute"

Language
VB.NET
Type
Bug
Severity
Major
Tags
mef, pitfall

Why is this an issue?

To customize the default behavior for an export in the Managed Extensibility Framework (MEF), applying the PartCreationPolicyAttribute is necessary. For the PartCreationPolicyAttribute to be meaningful in the context of an export, the class must also be annotated with the ExportAttribute.

This rule raises an issue when a class is annotated with the PartCreationPolicyAttribute but not with the ExportAttribute.

How to fix it

Code examples

Noncompliant code example


Imports System.ComponentModel.Composition

<PartCreationPolicy(CreationPolicy.Any)> ' Noncompliant
Public Class FooBar
    Inherits IFooBar
End Class

Compliant solution


Imports System.ComponentModel.Composition

<Export(GetType(IFooBar))>
<PartCreationPolicy(CreationPolicy.Any)>
Public Class FooBar
    Inherits IFooBar
End Class

Resources

Documentation

Articles & blog posts

↑ Back to top