2016-09-12 15 views
1

我正在嘗試構建一個C++/CLR包裝,以從C#.Net應用程序中調用我的C++代碼。.NET包裝到C++代碼:由於其保護級別錯誤,CS0122無法訪問

下面是步驟如下:

C++項目:

cppproject.h

#ifndef _CPPPROJECT_H 
#define _CPPPROJECT_H 


typedef enum { 
    SUCCESS, 
    ERROR 
} StatusEnum; 

namespace cppproject 
{ 
    class MyClass { 

    public: 
     MyClass(); 
     virtual ~MyClass(); 
     StatusEnum Test(); 

    }; 
} 

#endif 

cppproject.cpp

#include "cppproject.h" 

namespace cppproject { 

    MyClass::MyClass() {}; 
    MyClass::~MyClass() {}; 

    StatusEnum MyClass::Test() 
    { 
     return SUCCESS; 
    } 

} 

現在包裝項目(C++/CLR類型)綁在一起C#和C++:

wrapper.h

// wrapper.h 

#pragma once 

#include "cppproject.h" 

using namespace System; 

namespace wrapper { 

    public ref class Wrapper 
    { 
     public: 
      /* 
      * The wrapper class 
      */ 
      cppproject::MyClass* wrapper; 

      Wrapper(); 
      ~Wrapper(); 

      StatusEnum Test(); 
    }; 
} 

wrapper.cpp

// This is the main DLL file. 

#include "stdafx.h" 

#include "wrapper.h" 

namespace wrapper { 

    Wrapper::Wrapper() 
    { 
     wrapper = new cppproject::MyClass(); 
    } 

    Wrapper::~Wrapper() 
    { 
     delete wrapper; 
    } 

    StatusEnum Wrapper::Test() 
    { 
     return wrapper->Test(); 
    }; 
} 

最後的C#代碼,其中I'm得到錯誤:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using wrapper; 


namespace netproject 
{ 
    /* 
    * Enums 
    */ 
    public enum StatusEnum { 
     SUCCESS, 
     ERROR 
    }; 

    public partial class netproject 
    { 
     public const int MAX_REPORT_DATA_SIZE = 1024; 
     public wrapper.Wrapper wrapper; 

     public netproject() { wrapper = new wrapper.Wrapper(); } 
     ~netproject() { wrapper = null; } 

     public StatusEnum Test() 
     { 
      var sts = wrapper.Test(); <<- ERROR 
      return (netproject.StatusEnum) sts;<<- ERROR 
     } 
    } 
} 

在C#項目中的編譯器錯誤:

error CS0122: 'wrapper.Wrapper.Test()' is inaccessible due to its protection level 
error CS0426: The type name 'StatusEnum' does not exist in the type 'netproject.netproject' 

我不能明白。 Test在包裝器項目和C++項目中都定義爲public。並且StatusEnum也在錯誤行上方的C#項目中公開。

幫助appreaciated找出what's怎麼回事....

+0

您不能在C#程序中使用非託管枚舉類型。你必須在你的C++/CLI代碼中聲明一個'public enum class'。只需從int轉換即可。 –

+0

Re'netproject.netproject':[不要命名與其命名空間相同的類](https://blogs.msdn.microsoft.com/ericlippert/2010/03/09/do-not-name-a-class -the-相同-AS-其名稱空間部酮/)。 – dxiv

+0

感謝Hans,但是請您詳細說明一下或者指出一個例子......我的包裝中沒有enum ....演員在返回時在'(netproject.StatusEnum)'處完成...... – Mendes

回答

1
typedef enum { 
    SUCCESS, 
    ERROR 
} StatusEnum; 

這是不是就是在C#中訪問。正如我所看到的,您有兩種選擇:

1)您可以使枚​​舉成爲一個託管的枚舉。

public enum class StatusEnum { 
    SUCCESS, 
    ERROR 
}; 

2)我通常不是隻有兩個值的枚舉迷。在許多情況下,布爾運算也是一樣。

public ref class Wrapper 
{ 
    // returns true on success. 
    bool Test(); 
}; 
+0

謝謝大衛。此代碼是一個隔離錯誤的示例代碼 - 我將它簡化爲一組具有相同行爲的項目。我真正的應用程序有幾個枚舉數十個選項....問題:public enum類StatusEnum將在C++/CLI代碼中出現?我的C#會看到這個枚舉(它們將在不同的命名空間中:'wrapper') – Mendes

相關問題