简体   繁体   中英

How to use a C# Enum in C++/CLI

I have a C# assembly containing an enum:

public enum MyEnum
{
    OK = 0, 
    NOT_OK = -1
}

I've added the reference to the C++/CLI project but I don't know how to return a value from a function.

Header:

#pragma once

using namespace System;
using namespace MyNamespace;

namespace NativeWrapper {

    public ref class API
    {
        public:
            static MyNamespace::MyEnum Go();
    };

}

CPP:

#include "StdAfx.h"
#include "API.h"

using namespace NativeWrapper;

MyNamespace::MyEnum API::Go()
{
    return MyEnum.OK;
}

I get the following when I build the project:

error C2275: 'MyNamespace::MyEnum' : illegal use of this type as an expression

You need colons, not a period.

MyNamespace::MyEnum API::Go()
{
    return MyEnum::OK;
}

Also, note that if MyEnum was originally written in C++/CLI, you'd need to declare it as public enum class MyEnum { ... } to make it a managed enum that you can use from C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM