在解决方案中将 MEF 与多个项目结合使用

发布于 2024-09-25 23:48:39 字数 512 浏览 1 评论 0原文

我有一个具有以下结构的解决方案:

解决方案

<块引用>

主执行程序

实用程序

当我在实用程序项目中使用 MEF 时,我发现以下 MEF 目录都无法获取 Main Exe 中保存的类型,

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

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

我假设第一个在实用程序项目中调用时失败,而第二个由于类型而失败主项目中的内容存储在 EXE 而不是 DLL 中...

获取可查找解决方案的所有项目中的所有类型的 Mef 目录的正确方法是什么?

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

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...

what is the correct way to get a Mef catalogue which finds all types in all the projects of a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-10-02 23:48:39

在我的项目中,我只需在构建后事件中放置一个命令,该命令将输出复制到主应用程序 bin 目录中:

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

然后我只需从根目录路径加载:

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

In my projects I simply put a command in the post-build events, which copies the output into the main apps bin directory:

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);
单挑你×的.吻 2024-10-02 23:48:39

刚刚发现 Assembly.GetEntryAssembly() 因此以下内容将起作用

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

,尽管它不会加载解决方案中的其他类型 - 一种选择是使用目录目录,但我想知道是否有某种方法可以避免使用它,因为它增加了可能性在我不立即需要的地方引入第三方类型?

Just Spotted Assembly.GetEntryAssembly() so the following will work

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文