问题
What does [PartCreationPolicy(CreationPolicy.Shared)] mean?
回答1:
To add to Julien's answer, I think conceptually you can think of it as a Singleton.
回答2:
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
}
来源:https://stackoverflow.com/questions/12557039/what-does-partcreationpolicycreationpolicy-shared