作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确信这很简单,但我就是无法弄清楚。
我想通过传入枚举的成员之一来返回枚举的类型名称。
我的所有搜索都显示了如何从 int 值获取成员名称,或从字符串值显示名称等。但我找不到任何可以显示如何完成此操作的内容。
string name1 = GetEnumName(Colour.Black)
// should be "Colour"
string name2 = GetEnumName(Flower.Daisy)
//should be "Flower"
public static string GetEnumName(Enum myEnum)
{
// ...what goes here...?
}
public enum Colour
{
Black, White, Pink, Blue
}
public enum Flower
{
Pansy, Daffodil, Daisy
}
最佳答案
您可以使用.GetType().Name
。
GetType()
获取当前实例的类型。它具有 Name
属性。请记住,您还可以使用 .GetType().ToString()
或 .GetType().FullName
它将返回对象类型的 namespace +名称.
另外不要忘记,您可以使用 is
关键字来确定对象是否为指定类型。 F.e:
if(myEnum is Flower)
关于c# - 如何从枚举类型的值之一获取字符串形式的枚举类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043249/
我是一名优秀的程序员,十分优秀!