Why is this an issue?
Marking a class with PartCreationPolicy
(CreationPolicy.Shared
), which is
part of Managed Extensibility Framework (MEF), means that a single, shared
instance of the exported object will be created. Therefore it doesn’t make sense to create new instances using the constructor and it will most likely
result in unexpected behaviours.
This rule raises an issue when a constructor of a class marked shared with a PartCreationPolicyAttribute
is invoked.
How to fix it
Code examples
Noncompliant code example
<Export(GetType(IFooBar))>
<PartCreationPolicy(CreationPolicy.[Shared])>
Public Class FooBar
Inherits IFooBar
End Class
Public Class Program
Public Shared Sub Main()
Dim fooBar = New FooBar() ' Noncompliant
End Sub
End Class
Compliant solution
<Export(GetType(IFooBar))>
<PartCreationPolicy(CreationPolicy.[Shared])>
Public Class FooBar
Inherits IFooBar
End Class
Public Class Program
Public Shared Sub Main()
Dim fooBar = serviceProvider.GetService(Of IFooBar)()
End Sub
End Class
Resources
Documentation