What does [PartCreationPolicy(CreationPolicy.Shared)] mean?
It means that, when requesting an instance of a class decorated with [PartCreationPolicy(CreationPolicy.Shared)]
, the CompositionContainer
will always return the same instance of this class and not create a new one.
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
class Foo
{
}
The above class will give the following result:
private void Test()
{
var foo1 = Container.GetExportedValue<Foo>();
var foo2 = Container.GetExportedValue<Foo>();
Console.WriteLine(foo1 == foo2); // => True
}
To add to Julien's answer, I think conceptually you can think of it as a Singleton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With