我有问题的方法Enum::GetValues()。它什么也不返回——没有错误,没有元素。a的长度是0。这个代码来自微软网站,所以这是官方的例子。我做不到。项目是为。net框架4.7.2,这应该是好的。我使用VS2022与所有更新。有人有什么建议吗?由于
链接:https://learn.microsoft.com/en - us/dotnet/api/system.enum.getvalues?view=net - 6.0
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow
};
int main()
{
Console::WriteLine( "The values of the Colors Enum are:" );
Array^ a = Enum::GetValues( Colors::typeid );
for ( Int32 i = 0; i < a->Length; i++ )
{
Object^ o = a->GetValue( i );
Console::WriteLine( "{0}", Enum::Format( Colors::typeid, o, "D" ) );
}
}
这个例子应该产生:
// The values of the Colors Enum are:
// 0
// 1
// 2
// 3
但是结果只有:
The values of the Colors Enum are:
enum class
是c++11范围内的枚举,而不是。net枚举,参见https://learn.microsoft.com/en-us/cpp/dotnet/how-to-define-and-consume-enums-in-cpp-cli
的说明要使其成为。net enum,您需要添加private
或public
。例如:
private enum class Colors
{
Red, Green, Blue, Yellow
};