org.sonar.plugins.csharp.S4428.html Maven / Gradle / Ivy
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
using System.ComponentModel.Composition;
[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant
public class FooBar : IFooBar { }
Compliant solution
using System.ComponentModel.Composition;
[Export(typeof(IFooBar))]
[PartCreationPolicy(CreationPolicy.Any)]
public class FooBar : IFooBar { }
Resources
Documentation
- Microsoft Learn - Managed Extensibility Framework (MEF)
- Microsoft Learn - Attributed
programming model overview (MEF)
- Microsoft Learn - PartCreationPolicyAttribute
Class
- Microsoft Learn - ExportAttribute
Class
- Microsoft Learn - CreationPolicy
Enum
Articles & blog posts
- Stefan Henneken - MEF Part 1 – Fundamentals,
Imports and Exports
- Stefan Henneken - MEF Part 2 – Metadata and creation
policies
- Stefan Henneken - MEF Part 3 – Life cycle
management and monitoring