diff --git a/rules/S4428/csharp/rule.adoc b/rules/S4428/csharp/rule.adoc index 2b39cb9f06..aeec1d5f70 100644 --- a/rules/S4428/csharp/rule.adoc +++ b/rules/S4428/csharp/rule.adoc @@ -1,29 +1,32 @@ -== Why is this an issue? - include::../description.adoc[] -=== Noncompliant code example +== How to fix it -[source,csharp] +=== Code examples + +==== Noncompliant code example + +[source,csharp,diff-id=1,diff-type=noncompliant] ---- +using System.ComponentModel.Composition; + [PartCreationPolicy(CreationPolicy.Any)] // Noncompliant -public class FooBar : IFooBar -{ -} +public class FooBar : IFooBar { } ---- -=== Compliant solution +==== Compliant solution -[source,csharp] +[source,csharp,diff-id=1,diff-type=compliant] ---- +using System.ComponentModel.Composition; + [Export(typeof(IFooBar))] [PartCreationPolicy(CreationPolicy.Any)] -public class FooBar : IFooBar -{ -} +public class FooBar : IFooBar { } ---- +include::../resources.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S4428/description.adoc b/rules/S4428/description.adoc index 95d997e590..8024b45d7f 100644 --- a/rules/S4428/description.adoc +++ b/rules/S4428/description.adoc @@ -1,4 +1,6 @@ -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. +== Why is this an issue? +To customize the default behavior for an export in the https://learn.microsoft.com/en-us/dotnet/framework/mef/[Managed Extensibility Framework] (MEF), applying the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[`PartCreationPolicyAttribute`] is necessary. +For the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[`PartCreationPolicyAttribute`] to be meaningful in the context of an export, it is required to also annotate the class with the https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.exportattribute[`ExportAttribute`]. -This rule raises an issue when a class is marked as shared with a ``++PartCreationPolicyAttribute++`` but lacks a ``++ExportAttribute++``. +This rule raises an issue when a class is annotated with the `PartCreationPolicyAttribute` but not with the `ExportAttribute`. diff --git a/rules/S4428/metadata.json b/rules/S4428/metadata.json index 355f30bea7..7b26824214 100644 --- a/rules/S4428/metadata.json +++ b/rules/S4428/metadata.json @@ -25,5 +25,5 @@ "defaultQualityProfiles": [ "Sonar way" ], - "quickfix": "unknown" + "quickfix": "targeted" } diff --git a/rules/S4428/resources.adoc b/rules/S4428/resources.adoc new file mode 100644 index 0000000000..d15d839528 --- /dev/null +++ b/rules/S4428/resources.adoc @@ -0,0 +1,14 @@ +== Resources + +=== Documentation +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/framework/mef/[Managed Extensibility Framework (MEF)] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/framework/mef/attributed-programming-model-overview-mef[Attributed programming model overview (MEF)] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.partcreationpolicyattribute[PartCreationPolicyAttribute Class] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.exportattribute[ExportAttribute Class] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.composition.creationpolicy[CreationPolicy Enum] + +=== Articles & blog posts + +* Stefan Henneken - https://stefanhenneken.net/2015/11/08/mef-part-1-fundamentals-imports-and-exports/[MEF Part 1 – Fundamentals, Imports and Exports] +* Stefan Henneken - https://stefanhenneken.net/2019/01/26/mef-part-2-metadata-and-creation-policies/[MEF Part 2 – Metadata and creation policies] +* Stefan Henneken - https://stefanhenneken.net/2019/03/06/mef-part-3-life-cycle-management-and-monitoring/[MEF Part 3 – Life cycle management and monitoring] diff --git a/rules/S4428/vbnet/rule.adoc b/rules/S4428/vbnet/rule.adoc index 709e406784..d6457dff15 100644 --- a/rules/S4428/vbnet/rule.adoc +++ b/rules/S4428/vbnet/rule.adoc @@ -1,21 +1,27 @@ -== Why is this an issue? - include::../description.adoc[] -=== Noncompliant code example +== How to fix it -[source,vbnet] +=== Code examples + +==== Noncompliant code example + +[source,vbnet,diff-id=1,diff-type=noncompliant] ---- +Imports System.ComponentModel.Composition + ' Noncompliant Public Class FooBar Inherits IFooBar End Class ---- -=== Compliant solution +==== Compliant solution -[source,vbnet] +[source,vbnet,diff-id=1,diff-type=compliant] ---- +Imports System.ComponentModel.Composition + Public Class FooBar @@ -23,6 +29,8 @@ Public Class FooBar End Class ---- +include::../resources.adoc[] + ifdef::env-github,rspecator-view[] '''