相当于 C++/CLI 中枚举类型的 ToString()
在 C# 中,您可以声明一个枚举,设置其值后,可以调用该变量的 ToString 并获取该枚举值的字符串表示形式。
如何使用托管枚举在 C++/CLI 中执行此操作?
In C# you can declare an enum and once you have set its value call ToString on the variable and get a string representation of the value of the enum.
How do you do this in C++/CLI using a managed enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然其他答案并没有错,但我发现自己也遇到了同样的问题。就我而言,我声明了一个标准 C++ 枚举,但忘记使用 CLI 语法(即使我已将其公开在公共属性中而没有编译器警告!)。
C++/CLI 枚举的正确语法是(注意“class”一词):
注意:您还可以使用“struct”而不是“class”。
您还需要返回代码并更改任何分配(但是编译器会很高兴地向您显示错误),从: 更改为
:
您现在会发现“ToString()”按照您的预期工作。
Whilst the other answers are not incorrect, I found myself with the same problem. In my case, I had declared a standard C++ enum and forgot to use the CLI syntax (even though I had it been exposed in public properties without compiler warnings!).
The proper syntax for a C++/CLI enum is (Note the word 'class'):
NB: You can also use 'struct' rather than 'class'.
You will also need to go back through your code and change any assignments (the compiler will happily show you the errors however) from this:
to this:
You will now find that 'ToString()' works as you'd expect it to.
语法不是一样吗?
请参阅 Google 图书
Isn't it the same syntax ?
Look at this page of the book "Pro Visual C++/CLI and the .NET 3.5 Platform" in Google Books
ToString() 应该返回枚举值的名称。此外,如果您使用 [Flags] 属性装饰托管枚举,则例如
Colour::Red | Colour::Blue
将 ToString() 视为“红、蓝”。 (这是根据Marcus Heege的书“Expert Visual C++/CLI: .NET for Visual C++ Programmers”的记忆,我自己没有尝试过)ToString() should return the name of the enum value. Furthermore, if you decorate the managed Enum with a [Flags] attribute, then eg
Colour::Red | Colour::Blue
will ToString() as "Red, Blue". (This is from memory from Marcus Heege's book "Expert Visual C++/CLI: .NET for Visual C++ Programmers", not tried it myself)您的问题可能已得到解决。
我认为其他人可能会发现它有用:
NOT:成员数量最多可达 96 个。( ENUM96.inl )
结果如下所示。
Your issue was likely resolved.
I thought someone else might find it useful:
NOT: The number of members can be up to 96. ( ENUM96.inl )
The result will be as shown below.