我在MEF一個初學者,所以我有一個問題:) 我有以下幾點:MEF懶ImportMany與Creationpolicy.NonShared
[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實例嗎?我總是一樣的。
thx爲ExportFactory提示。我會試一下 :) – blindmeis 2010-07-09 08:12:00