我正在尝试理解MEF的构造函数注入属性。我不知道如何告诉它加载构造函数的参数。
我要加载的是这个属性
[ImportMany(typeof(BUsers))]
public IEnumerable<BUsers> LoadBUsers { get; set; }
这是我用来导入程序集的代码。
try
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog("DI"));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
这里是我尝试加载的类
[Serializable]
[Export(typeof(BUsers))]
public class EditProfile : BUsers
{
[ImportingConstructor]
public EditProfile(string Method, string Version)
{
Version = "2";
Action = "Edit";
TypeName = "EditProfile";
}
OnImportsSatisfied
调用之间没有拦截的方法来设置这个_名称_ - 但在我的情况下,在OnImportsSatisfied
中访问配置是有效的。 - 我对你的回答还有另一个问题:container.ComposeParts(this)
中的_this_是什么? 我不认为这是当前创建的EditProfile
实例。 - Sebastian Schumann