MEF Lazy ImportMany with Creationpolicy.NonShared
我是 MEF 的初学者,所以我有一个问题:) 我有以下内容:
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(SharedExport))]
public class SharedExport : INPCBase
{
[ImportMany(typeof(INonShared),RequiredCreationPolicy = CreationPolicy.NonShared)]
private IEnumerable<Lazy<INonShared,Dictionary<string,object>>> fac;
...
public void Open()
{
foreach (var lazy in fac)
{
this.Muster.Add(lazy.Value);
}
}
导入的类全部标记为非共享。
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(INonShared))]
[ExportMetadata("Muster","030")]
public sealed class NonShared1 : INPCBase, INonShared
{
public NonShared1()
{
Debug.WriteLine("ctor NonShared1" + this.GetHashCode().ToString());
}
#region Implementation of INonShared
public string Displayname
{
get { return "Muster 030 "+ this.GetHashCode().ToString();
}
}
#endregion
}
现在我的问题:当 Open() 执行时,不应该总是创建一个新的 NonShared1 实例吗?我总是一样。
i'm a beginner in mef and so i have a question :)
i have the following:
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(SharedExport))]
public class SharedExport : INPCBase
{
[ImportMany(typeof(INonShared),RequiredCreationPolicy = CreationPolicy.NonShared)]
private IEnumerable<Lazy<INonShared,Dictionary<string,object>>> fac;
...
public void Open()
{
foreach (var lazy in fac)
{
this.Muster.Add(lazy.Value);
}
}
the imported classes all marked as nonshared.
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(typeof(INonShared))]
[ExportMetadata("Muster","030")]
public sealed class NonShared1 : INPCBase, INonShared
{
public NonShared1()
{
Debug.WriteLine("ctor NonShared1" + this.GetHashCode().ToString());
}
#region Implementation of INonShared
public string Displayname
{
get { return "Muster 030 "+ this.GetHashCode().ToString();
}
}
#endregion
}
now my question: when Open() execute, shouldn't there always a new NonShared1 instance be created? i got always the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
马修关于共享/非共享方面仅影响每次导入时给出的实例的说法是正确的,每次拉取 Lazy.Value 时都不会获得新实例。如果您想要每次获取一个新实例并处理它,您可能会考虑使用 ExportFactory。目前 ExportFactory 仅存在于 Silverlight 版本的 MEF 中,但如果您确实需要此功能,mef.codeplex.com 上有一个示例项目,可以将该功能添加到桌面版本的 MEF 中。
Matthew is correct about the Shared/NonShared aspect only affecting the instance given at each import, you will not get a new instance every time you pull on Lazy.Value. If what you want is to get a new instance each time and dispose it you might look into using ExportFactory. Currently ExportFactory only exists in the Silverlight version of MEF but there is a sample project on mef.codeplex.com that adds the functionality to the desktop version of MEF if you really need this functionality.
不,因为 Lazy<>实例。
Lazy
专为延迟加载值而设计。该值是在您第一次访问.Value
属性时创建的,此后对该属性的所有访问都会返回相同的实例。 NonShared/Shared 创建策略在导入过程中发挥作用,因此通过属性注入、构造函数注入、字段注入、.GetExportedValue
等...No, because of the Lazy<> instance. A
Lazy<T>
is designed for lazy loading of a value. The value is created the first time you access the.Value
property, and the same instance is returned for all access to that property thereafter. The NonShared/Shared creation policy comes into play doing the import process, so through property injection, constructor injection, field injection, .GetExportedValue
etc...