在C ++ \ CLI中转换枚举的问题

时间:2022-09-01 11:32:13

I have an assembly, written in C++\CLI, which uses some of enumerations, provided by .Net. It has such kind of properties:

我有一个用C ++ \ CLI编写的程序集,它使用.Net提供的一些枚举。它有这样的属性:

property System::ServiceProcess::ServiceControllerStatus ^ Status  
{  
    ServiceControllerStatus ^ get()  
    {  
        return (ServiceControllerStatus)_status->dwCurrentState;   
    }  
}    

it works fine, but when i use this assembly from my C# code, type of this property is

它工作正常,但当我从我的C#代码使用此程序集时,此属性的类型是

System.Enum

and i have to make type-cast

我必须进行类型转换

 if ((ServiceControllerStatus)currentService.Status == ServiceControllerStatus.Running)
     //do smth

The question is simple: why is it so, and how to fix it ?

问题很简单:为什么会这样,以及如何解决它?

2 个解决方案

#1


5  

In C++/CLI ^ is like the analagous * in standard C++. Because enumerations are value types the ^ should not be included otherwise you will see them as System.Enum.

在C ++ / CLI中,^就像标准C ++中的analagous *。因为枚举是值类型,所以不应包含^,否则您将看到它们为System.Enum。

Remove the ^ and you will see the correct enumeration on C# side.

删除^,您将在C#端看到正确的枚举。

property System::ServiceProcess::ServiceControllerStatus Status  
{  
    System::ServiceProcess::ServiceControllerStatus get()  
    {  
        return (System::ServiceProcess::ServiceControllerStatus)_status->dwCurrentState;   
    }  
}

#2


3  

I think enums don't use the ^ -- try removing it from the property declaration and get().

我认为枚举不使用^ - 尝试从属性声明和get()中删除它。

#1


5  

In C++/CLI ^ is like the analagous * in standard C++. Because enumerations are value types the ^ should not be included otherwise you will see them as System.Enum.

在C ++ / CLI中,^就像标准C ++中的analagous *。因为枚举是值类型,所以不应包含^,否则您将看到它们为System.Enum。

Remove the ^ and you will see the correct enumeration on C# side.

删除^,您将在C#端看到正确的枚举。

property System::ServiceProcess::ServiceControllerStatus Status  
{  
    System::ServiceProcess::ServiceControllerStatus get()  
    {  
        return (System::ServiceProcess::ServiceControllerStatus)_status->dwCurrentState;   
    }  
}

#2


3  

I think enums don't use the ^ -- try removing it from the property declaration and get().

我认为枚举不使用^ - 尝试从属性声明和get()中删除它。