我猜你正在打电话CompositionContainer.ComposeParts(this),哪里this有类似这样的财产:
[ImportMany]
public IPlugin[] Plugins { get; set; }
Run Code Online (Sandbox Code Playgroud)
这意味着当你调用时ComposeParts,将调用所有插件的构造函数.或者,您可以利用延迟加载,这将在您实际使用插件时将构造函数调用推迟
[ImportMany]
public Lazy<IPlugin>[] Plugins { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后,如果你想初始化所有插件,你可以有这样的东西,它将记录异常,但不会阻止你加载其他插件:
public void InitPlugins()
{
foreach (Lazy<IPlugin> lazyPlugin in Plugins)
{
try
{
// Call the plugin's constructor
var plugin = lazyPlugin.Value;
// Do any other initialization here
}
catch (Exception ex)
{
// Log exception and continue iteration
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |