C++/CLI 托管C++之enum枚举封装【6】

时间:2022-09-01 18:02:09

CLI封装枚举类型时,使用eum class/struct。


【1】C++封装

enum  DataEnum
{
DATA_READ = 0,
DATA_WRITE,
};
//2.1 枚举类型作为输入参数EXPORTDLL_CLASS int Enum_Output(DataEnum  type){    wprintf(L"Enum_Output %d\n", type);    return(0);}//2.2 枚举类型作为输出参数EXPORTDLL_CLASS int Enum_Change(DataEnum  &type){    type = DataEnum::DATA_WRITE;    wprintf(L"Enum_Change %d\n", type);    return(0);}

【2】CLI封装

/// <summary>
/// 枚举类型
/// </summary>
[Serializable]
[FlagsAttribute]
public enum class DataType
{
READ = 0,
WRITE,
};
/// <summary>
/// 2 枚举类型测试类
/// </summary>
public ref class EnumCls
{
public:
/// <summary>
/// 2.1 枚举类型作为输入参数
/// </summary>
/// <param name="type">枚举类型</param>
/// <returns>成功返回0</returns>
static Int32 EnumOutput(DataType type);
/// <summary>
/// 2.2 枚举类型作为输出参数
/// </summary>
/// <param name="type">枚举类型</param>
/// <returns>成功返回0</returns>
static Int32 EnumChange(DataType %type);
};
System::Int32 ExportCLI::EnumCls::EnumOutput( DataType type ){    return( Enum_Output(safe_cast<DataEnum>(type)) );}System::Int32 ExportCLI::EnumCls::EnumChange( DataType %type ){    DataEnum   t;    HRESULT hr = Enum_Change(t);    type = safe_cast<DataType>(t);        return(hr);}

【3】C#测试程序

//2. 枚举类型测试类
DataType tEnum = DataType.READ;

EnumCls.EnumOutput(tEnum);
EnumCls.EnumChange(ref tEnum);

源码地址:

https://coding.net/u/aoshilangCode/p/CLI/git