3
votes

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.

1

1 Answers

4
votes

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?