[英]using MEF in a ClassLibrary
我想在ClassLibrary中使用MEF,而不是在應用程序項目中使用(無論是ConsoleApplication還是WebApplication ......)。
當我嘗試這樣做時(如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中始終為“null”的“插件”屬性。
任何人都有解決方案嗎?
這可能只是您的目錄的問題。 如果要將屬性移動到類庫中,則意味着單獨的程序集。 目前,只有托管“程序”的程序集在您的目錄中,以及“擴展程序”中的任何內容。 那么,你確定你的新類庫在擴展文件夾中嗎?
另外,你構成了this
意思,你正在編寫一個“程序”實例如果你將你的屬性移動到另一個庫中的另一個類,那么你需要編寫一個任何類的實例。
像這樣的東西:
class Program
{
private CompositionContainer _container;
private Program()
{
var helper = new MyOtherHelperClass();
var catalog = helper.CreateCatalog();
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
//Fill the imports of some object
try
{
var thingIWantToCompose = new OtherClassWithAnImport();
this._container.ComposeParts(thingIWantToCompose);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program(); //Composition is performed in the constructor
}
}
如果這不起作用,那么嘗試在Visual MEFX中查看您的部件。 以下是設置它的簡短指南: Visual MEFX入門
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.