我正在使用 MEF 2 开发一个应用程序。我想尝试无属性的插件设计方式,现在我试图弄清楚为什么我的一个对象似乎不满足其导入。下面是示例代码。
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Registration;
using System.Reflection;
namespace MEF2
{
class Program
{
static void Main(string[] args)
{
var builder = new RegistrationBuilder();
builder.ForType<ExporterClass>().Export<ExporterClass>();
builder.ForType<ImporterClass>().Export().ImportProperties(p => p.Name == "Exporter");
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder);
CompositionContainer container = new CompositionContainer(catalog);
ImporterClass t = new ImporterClass();
container.ComposeParts(t);
t.ShowWalkers();
}
}
public class ExporterClass
{
public void Write()
{
Console.WriteLine("Test");
}
}
public class ImporterClass : IPartImportsSatisfiedNotification
{
//[Import]
private ExporterClass Exporter { get; set; }
public void ShowWalkers()
{
if (Exporter != null)
Exporter.Write();
}
public void OnImportsSatisfied()
{
Console.WriteLine("IPartImportsSatisfiedNotification");
}
}
}
OnInputSatisfied
事件被触发,但 Exporter 属性仍然为空。如果我取消注释[Import]
属性一切正常。
我应该怎么做才能导入没有属性?