如何在多个托管C ++程序集之间共享接口. [英] How to share an interface between multiple managed C++ assemblies.
问题描述
我有一个定义接口的托管C ++基本程序集(BaseAssembly):
I''ve got a managed C++ base assembly (BaseAssembly) that defines an interface:
// In MyInterface.h
namespace BaseAssembly
{
public interface class IMyInterface
{
//...
};
}
其他两个托管的C ++程序集每个都包含一个实现此接口的类:
Two other managed C++ assemblies each contain a class that implements this interface:
// MyDerived1.h in managed C++ project MyDerived1Lib
#include "MyInterface.h";
namespace DerivedAssembly1
{
public ref class MyDerived1 : public BaseAssembly::IMyInterface
{
//...
};
}
// MyDerived2.h in managed C++ project MyDerived2Lib
#include "MyInterface.h";
namespace DerivedAssembly2
{
public ref class MyDerived2 : public BaseAssembly::IMyInterface
{
//...
};
}
当我尝试在单独的C#应用程序中创建IMyInterfaces实例时,出现错误CS0433:MyDerived1.dll和MyDerived2.dll中都存在BaseAssembly.IMyInterface类型.我不确定如何继续.
以下是令人反感的C#代码的片段:
When I try to create an instance of IMyInterfaces in a separate C# app, I get error CS0433: The type BaseAssembly.IMyInterface exists in both MyDerived1.dll and MyDerived2.dll. Im not sure how to proceed.
Heres a snippet of the offending C# code:
// In MyClientApp.cs
namespace MyApp
{
public class MyClientApp
{
public void Initialize()
{
IMyInterface impl = new MyDerived1();
}
}
}
C#项目引用了两个派生的程序集.我尝试将基础程序集保留为裸头文件(即,不将其编译为DLL),然后将其编译为DLL并从C#项目中引用它.似乎没有任何作用.预先感谢您的帮助.
The C# project references both derived assemblies. I''ve tried leaving the base assembly as a bare header file (i.e. not compiling it to a DLL), and compiling it to a DLL and referencing it from the C# project. Nothing seems to work. Thanks in advance for your help.
推荐答案
尝试使用
#pragma once
在头文件MyInterface.h中.
也许可以解决问题.
Try to use
#pragma once
in the header file MyInterface.h.
That might solve the problem.
有2个选项:
1-创建一个其他DLL,它将包含您的基本接口.并在其他程序集中引用该dll(包含实现该接口的派生类的dll和exe)
2-使您现有的dll之一引用另一个将包含该接口的dll.
There are 2 options:
1- Create a nother dll which will contain your base interface. And reference this dll in your other assemblies (the dll containing the derived classes implementing this interface, and the exe)
2- Make one of your existing dlls reference the other one which will contain the interface.
这篇关于如何在多个托管C ++程序集之间共享接口.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!