我想在ClassLibrary中使用MEF,而不是在應用程序項目中使用(既不是ConsoleApplication也不是WebApplication ...)。在類庫中使用MEF
當我嘗試這樣做(如MSDN:http://msdn.microsoft.com/en-us/library/dd460648.aspx)
class Program
{
private CompositionContainer _container;
[Import(typeof(IPlugin))]
public IPlugin Plugins;
private Program()
{
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog(@".\Extensions"));
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program(); //Composition is performed in the constructor
}
}
物業:
[Import(typeof(IPlugin))]
public IPlugin Plugins;
工作正常。
編輯:
不過,我想移動進口財產&總目錄下建立一個ClassLibrary,而不是在我的ConsoleApplication。
像下面這樣的東西。
在ClassLibrary [Manager.dll]:
public class Manager
{
private CompositionContainer _container;
[Import(typeof(IPlugin))]
public IPlugin Plugins;
public Manager()
{
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the Program class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Manager).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog(@".\Extensions"));
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
}
而在ConsoleApplication:
class Manager
{
static void Main(string[] args)
{
Manager m = new Manager(); //Composition is performed in the constructor
}
}
但是,當我做到這一點變化,我不能夠得到 「插件」 財產在我的ClassLibrary中總是「空」。
任何人都有解決方案來做到這一點?
簡單地把所有的插件放到類庫中,然後在你的程序中添加一個引用和命名空間聲明有什麼問題嗎?我的印象是,MEF管道本身(包括進口)需要成爲主要應用,才能正常運行。 –
請參閱「[堆棧溢出不需要你的SEO技巧](http://meta.stackexchange.com/a/130208/76337)」。 –
@RobertHarvey:我不想把我所有的插件放在同一個類庫中,也不能。我同意你關於你的感受......我認爲導入需要在它的主要應用程序中正常工作,但我認爲這不是一個好的想法,因爲它可能是複製代碼的來源。 – Sam