C ++ / CLI从本​​地C ++类,抽象方法继承并将其提供给C#提供给、抽象、方法、并将其

2023-09-03 17:06:43 作者:?? ? ? ? 【爱人权志

我一直在谷歌上搜索转转试图找到一个完全成熟的例子,这一点,但都无济于事。

I've been googling around in circles trying to find an fully fledged example to this but to no avail.

我有一个C ++的API,presents了一些含有纯虚函数为开发者从扩展类。我正在试图做的是通过C ++ / CLI提供此接口为C#。

I have a C++ API that presents a number of classes that contain pure virtual methods for the developer to extend from. What I'm attempting to do is provide this interface to C# via the C++/CLI.

我设法编译成C ++ / CLI库中的API,我已经到了一个关键点,因为我是新来的这一点。

I've managed to get the API compiled into the C++/CLI library and I've reached a sticking point as I'm new to this.

我知道,我需要创建一个包装到C ++ / CLI非托管类公开到托管.NET类,但我还没有找到一个坚实的例子和讨论,说明如何用一个抽象的c执行此++类。

I'm aware that I need to create a wrapper to expose the C++/CLI unmanaged class to a managed .net class but I've not found a solid example or discussion that shows how to do this with the an abstract C++ class.

任何人都可以点我在正确的方向,以一个完整的例子包括C#测试应用程序,显示结束了如何创建包装一个抽象类结束。这似乎是一个哦,你只是做X的事情,但我不能找出X是:)。我已经看到了这里的几个例子,但他们也不是很清楚。它已经3年左右,因为我解决任何C#。

Can anyone point me in the right direction to a full example including C# test application that shows the end to end of how to create the wrapper for an abstract class. It appears to be a "Oh you just do X" thing but I can't find out what X is :). I've seen a few examples on here but they aren't very clear. It's been about 3 years since I tackled any C#.

希望有人能帮助!

Sammich

推荐答案

使用code在发表汉斯帕桑特为基地的联系,以下是我认为你正在寻找。它不会编译这样的,因为我写这个例子中内联 - 把所有的方法实现在.cpp文件,然后你应该在正确的轨道

Using the code in the link posted by Hans Passant as a base, the following is what I think you are looking for. It won't compile like this because I have written this example 'inline' - put all the method implementations in the .cpp file and you should then be on the right track.

#pragma managed(push, off)
#include "oldskool.h"
#pragma comment(lib, "oldskool.lib")
#pragma managed(pop)

using namespace System;

ref class Wrapper; // You need a predeclaration to use this class in the
                   // constructor of OldSkoolRedirector.

// Overrides virtual method is native class and passes to wrapper class
class OldSkoolRedirector : public COldSkool {
public:
    OldSkoolRedirector(Wrapper ^owner) : m_owner(owner) { }
protected:
    virtual void sampleVirtualMethod() { // override your pure virtual method
        m_owner->callSampleVirtualMethod(); // body of method needs to be in .cpp file
    }
private:
    gcroot<Wrapper^> m_owner;
}

public ref class Wrapper abstract {
private:
    COldSkool* pUnmanaged;
public:
    Wrapper() { pUnmanaged = new OldSkoolRedirector(this); }
    ~Wrapper() { this->!Wrapper(); }
    !Wrapper() {
        if (pUnmanaged) {
            delete pUnmanaged;
            pUnmanaged = 0;
        }
    }
protected:
    virtual void sampleVirtualMethod() = 0; // Override this one in C#
internal:
    void callSampleVirtualMethod(){ 
        if (!pUnmanaged) throw gcnew ObjectDisposedException("Wrapper");
        sampleVirtualMethod(); 
    }
};