从C ++/CLI到C#的托管枚举 [英] managed enum from C++/CLI to C#
本文介绍了从C ++/CLI到C#的托管枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下托管课程
I have the following managed class
namespace Managed_DLL
{
public ref class AController
{
public:
delegate void didChangeListenerStatus(tListenerStatus status);
....};
// header file
#if defined(_MANAGED)
namespace Managed_DLL
{
#define ENUMKEYWORD public enum
#else
#define ENUMKEYWORD typedef enum
#endif
ENUMKEYWORD tListenerStatus
{
kListenerStopped,
kListenerRunning,
kListenerFailed} ;
}
我已经在dll预处理程序定义中定义了_MANAGED
在C#应用程序中,我已在其中一个类中将didChangeListenerStatus定义为
I have defined the _MANAGED in the dll preprocessor defines
In C# app I have defined the didChangeListenerStatus in one of classes as
using Managed_DLL
....
public void didChangeListenerStatus(tListenerStatus status)
{
Console.WriteLine(" didChangeListnerStatus called");
}
当我编译C#应用程序时,出现以下错误
When I compile the C# app it gives the following error
Error 4 ''tListenerStatus'' is inaccessible due to its protection level Error 3 Inconsistent accessibility: parameter type ''tListenerStatus'' is less accessible than method ''ADemo.ViewController.didChangeListenerStatus(tListenerStatus)
推荐答案
只需找到类型tListenerStatus
的声明并将其公开.
(请记住:C#和C ++/CLI之间没有互操作问题,对于所有.NET语言都相同.但是,C ++/CLI有一个特殊功能:引用类型的值语义",这不是可以用其他语言提供.换句话说,您只能使用"^"引用类型进行互操作.)
—SA
Just locate the declaration of the typetListenerStatus
and make it public.
(Remember: there is not inter-operation problems between C# and C++/CLI, the same stands for all .NET languages. However, there is one special feature of C++/CLI: the "value semantic" for reference types, which is not available in other languages. In other words, you can only use "^" reference types for inter-operation.)
—SA
我不认为您的预处理程序指令有效,并且正如其他答案所指出的那样,您不需要一个纯托管类.只需按照以下步骤声明头文件即可:
I don''t think your preprocessor directive is working, and as was pointed out in the other answer, you don''t need it for a purely managed class. Just make your header file declaration as follows:
namespace Managed_DLL
{
public enum tListenerStatus
{
kListenerStopped,
kListenerRunning,
kListenerFailed
};
}
这篇关于从C ++/CLI到C#的托管枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文