在C#中,可以使用'Enum.Parse'方法将'int'值转换为'enum'中的项。

以下是一个示例:

enum Colors
{
    Red,
    Green,
    Blue
}

int value = 1;
Colors color = (Colors)Enum.Parse(typeof(Colors), value.ToString());
Console.WriteLine(color); // 输出:Green

在上述示例中,我们将'int'值'1'转换为'enum'类型'Colors'中的项。我们首先将'int'值转换为字符串,然后使用'Enum.Parse'方法将字符串转换为'enum'项。输出结果为'Green',因为在'Colors'枚举中,'Green'对应的值是'1'。

C# 将整型值转换为枚举类型

原文地址: http://www.cveoy.top/t/topic/kgpv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录