An Issue with converting enumerations in C++\CLI
-
02-07-2019 - |
Question
I have an assembly, written in C++\CLI, which uses some of enumerations, provided by .Net. It has such kind of properties:
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
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 ?
Solution
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.
Remove the ^ and you will see the correct enumeration on C# side.
property System::ServiceProcess::ServiceControllerStatus Status
{
System::ServiceProcess::ServiceControllerStatus get()
{
return (System::ServiceProcess::ServiceControllerStatus)_status->dwCurrentState;
}
}
OTHER TIPS
I think enums don't use the ^ -- try removing it from the property declaration and get().
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow