Modify rule S4428: LaYC format (#2346)
This commit is contained in:
parent
0444d4df30
commit
be9c0bc4e8
@ -1,29 +1,32 @@
|
|||||||
== Why is this an issue?
|
|
||||||
|
|
||||||
include::../description.adoc[]
|
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
|
[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))]
|
[Export(typeof(IFooBar))]
|
||||||
[PartCreationPolicy(CreationPolicy.Any)]
|
[PartCreationPolicy(CreationPolicy.Any)]
|
||||||
public class FooBar : IFooBar
|
public class FooBar : IFooBar { }
|
||||||
{
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
|
include::../resources.adoc[]
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
ifdef::env-github,rspecator-view[]
|
||||||
|
|
||||||
|
@ -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`.
|
||||||
|
@ -25,5 +25,5 @@
|
|||||||
"defaultQualityProfiles": [
|
"defaultQualityProfiles": [
|
||||||
"Sonar way"
|
"Sonar way"
|
||||||
],
|
],
|
||||||
"quickfix": "unknown"
|
"quickfix": "targeted"
|
||||||
}
|
}
|
||||||
|
14
rules/S4428/resources.adoc
Normal file
14
rules/S4428/resources.adoc
Normal file
@ -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]
|
@ -1,21 +1,27 @@
|
|||||||
== Why is this an issue?
|
|
||||||
|
|
||||||
include::../description.adoc[]
|
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
|
||||||
|
|
||||||
<PartCreationPolicy(CreationPolicy.Any)> ' Noncompliant
|
<PartCreationPolicy(CreationPolicy.Any)> ' Noncompliant
|
||||||
Public Class FooBar
|
Public Class FooBar
|
||||||
Inherits IFooBar
|
Inherits IFooBar
|
||||||
End Class
|
End Class
|
||||||
----
|
----
|
||||||
|
|
||||||
=== Compliant solution
|
==== Compliant solution
|
||||||
|
|
||||||
[source,vbnet]
|
[source,vbnet,diff-id=1,diff-type=compliant]
|
||||||
----
|
----
|
||||||
|
Imports System.ComponentModel.Composition
|
||||||
|
|
||||||
<Export(GetType(IFooBar))>
|
<Export(GetType(IFooBar))>
|
||||||
<PartCreationPolicy(CreationPolicy.Any)>
|
<PartCreationPolicy(CreationPolicy.Any)>
|
||||||
Public Class FooBar
|
Public Class FooBar
|
||||||
@ -23,6 +29,8 @@ Public Class FooBar
|
|||||||
End Class
|
End Class
|
||||||
----
|
----
|
||||||
|
|
||||||
|
include::../resources.adoc[]
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
ifdef::env-github,rspecator-view[]
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user