What does [PartCreationPolicy(CreationPolicy.Shared)]

末鹿安然 提交于 2019-12-05 18:24:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!