简体   繁体   English

在解决方案中将MEF与多个项目一起使用

[英]Using MEF with multiple Projects in a Solution

I have a solution with the following structure: 我有以下结构的解决方案:

Solution

Main Exe 主要执行

Utilities 实用工具

When I use MEF within the Utilities project I find that neither of the following MEF catalogues pick up types held within the Main Exe 当我在Utilities项目中使用MEF时,我发现以下两个MEF目录都不选择Main Exe中保存的类型

catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

catalog.Catalogs.Add(new DirectoryCatalog(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory)));

i assume the first fails as its being called within the utilities project, and that the second fails since the types in the main project are stored in an EXE and not a DLL... 我假设第一个失败,因为它在实用程序项目中被调用,并且第二个失败,因为主项目中的类型存储在EXE中而不是DLL中...

what is the correct way to get a Mef catalogue which finds all types in all the projects of a solution? 获取在解决方案的所有项目中查找所有类型的Mef目录的正确方法是什么?

Just Spotted Assembly.GetEntryAssembly() so the following will work 只是发现Assembly.GetEntryAssembly()所以下面的工作

AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));  

although it does not load the other types in the solution - one option is to use a Directory catalogue though i wonder if there is some way to avoid using this as it raises the possibility of introducing 3rd party types where i dont immediatly want them? 尽管它不会加载解决方案中的其他类型-一种选择是使用目录目录,尽管我想知道是否有某种方法可以避免使用此目录,因为它增加了在我不想要它们的情况下引入第三者类型的可能性?

In my projects I simply put a command in the post-build events, which copies the output into the main apps bin directory: 在我的项目中,我只是在构建后事件中放置了一个命令,该命令将输出复制到主应用程序bin目录中:

XCOPY "$(ProjectDir)$(OutDir)*" "$(SolutionDir)MainApp\bin\*" /y

Then I just load from the root directory path: 然后,我只是从根目录路径加载:

AggregateCatalog Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new DirectoryCatalog(root_directory_path, "My.Assemlies.*"));
CompositionContainer Container = new CompositionContainer(Catalog);
Container.ComposeParts(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM