是否可以导出 C++带有 MEF 和 Prism 的 CLI 界面

发布于 2024-11-30 10:54:13 字数 770 浏览 1 评论 0原文

我有一个在 C++ 库中定义的 C++/CLI 接口(使用 /clr 开关编译) 我还有一个 C# 库。

我的 C# 库定义: - 实现 Prism IModule 的类界面。 - 实现 C++/CLI 接口并用 MEF 修饰的类导出属性。

C# 和 C++\CLI 库都部署到同一文件夹中。

我从 Prism 收到 ModuleLoadException,表示它找不到我的 C++/CLI 程序集或其依赖项之一。

如果我用 .NET 程序集替换 C++/CLI 程序集,一切都会正常!

那么我的问题是,是否可以导出一个实现 C++\CLI 接口的类,并且导出类型是该接口?

为什么我要在 C++/CLI 库中定义接口?我希望我们拥有的遗留 C++ DLL 实际上可以在 C++\CLI 库中定义它们的合约,并让 C# 库引用该合约 dll。 也许我的方法是错误的,如果您认为有更好的方法来实现这一点,请告诉我。

I have a C++/CLI interface defined in a C++ library (Compiled with the /clr switch)
I also have a C# library.

My C# library defines:
- a class that implements Prism IModule interface.
- a class that implements the C++/CLI interface and is decorated with MEF Export attribute.

both the C# and the C++\CLI library are deployed into the same folder.

I am getting a ModuleLoadException from Prism saying that it can't find my C++/CLI assembly or one of its dependencies.

If I replace the C++/CLI assembly with a .NET one, everything works fine!

My question is then , is it at all possible to export a class that implements a C++\CLI interface with the export type being that interface?

Why do I have the interface defined in a C++/CLI library? I was hoping that a legacy C++ DLL we have could actually define their contracts in that C++\CLI libraries and have C# libraries reference that contract dll.
Maybe my approach is wrong, please let me know if you think there is a better way to achieve this.

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

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

发布评论

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

评论(1

逆蝶 2024-12-07 10:54:13

我之前并没有花太多时间使用 C++/CLI,但由于它是一种符合 CLS 的语言,因此它应该可以正常工作。下面是一个示例

// CPPMEF_CPP.h

#pragma once

using namespace System;
using namespace System::ComponentModel::Composition;

namespace CPPMEF_CPP {

    [InheritedExportAttribute]
    public interface class ILogger
    {
    public:
        virtual void Log(System::Object^ obj) = 0;
    };
}

在我的 C# 控制台应用程序中:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using CPPMEF_CPP;

namespace CPPMEF_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            var logger = container.GetExportedValue<ILogger>();
            logger.Log("Test");

            Console.ReadKey();
        }
    }

    public class ConsoleLogger : ILogger
    {
        public void Log(object obj)
        {
            Console.WriteLine(obj);
        }
    }
}

部署 C++/CLI 程序集应该没有特殊要求,因为无论如何部署时都是一样的。您能否检查您的 C++/CLI 程序集是否也部署了任何依赖项?

I haven't really spent much time with C++/CLI before, but as it's a CLS-compliant language, it should just work. Here is an example

// CPPMEF_CPP.h

#pragma once

using namespace System;
using namespace System::ComponentModel::Composition;

namespace CPPMEF_CPP {

    [InheritedExportAttribute]
    public interface class ILogger
    {
    public:
        virtual void Log(System::Object^ obj) = 0;
    };
}

And in my C# console app:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using CPPMEF_CPP;

namespace CPPMEF_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            var logger = container.GetExportedValue<ILogger>();
            logger.Log("Test");

            Console.ReadKey();
        }
    }

    public class ConsoleLogger : ILogger
    {
        public void Log(object obj)
        {
            Console.WriteLine(obj);
        }
    }
}

There should be no special requirements for deploying C++/CLI assemblies, as it's all the same at deployment anyways. Can you check that any dependencies are deployed with your C++/CLI assembly too?

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