在 C# 中将枚举转换为字符串
本教程将讨论在 C# 中将枚举转换为字符串的方法。
在 C# 中用 Description
属性将枚举转换为字符串
对于遵循命名约定的简单 Enum 值,我们无需使用任何方法即可将其转换为字符串。可以使用 C# 中的 Console.WriteLine()
函数向用户显示。在下面的编码示例中进行了说明。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace enum_to_string {
public enum Status { InProgress, Completed }
class Program {
static void Main(string[] args) {
Status complete = Status.Completed;
Console.WriteLine(complete);
}
}
}
输出:
Completed
在上面的代码中,我们直接以 C# 的字符串格式打印 Enum 值 Completed
。这是可能的,因为我们的 Enum 值遵循 C# 中的变量命名约定。但是,如果要显示易于阅读的字符串,则必须在 C# 中使用 Enums 的 Description
属性。Description
属性用于描述枚举的每个值。我们可以通过在 Enum 的 Description
属性中编写字符串,将 Enum 转换为易于阅读的字符串。以下代码示例向我们展示了如何使用 C# 中的 Description
属性将 Enum 值转换为字符串。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace enum_to_string {
public enum Status {
[Description("The Desired Task is InProgress")] InProgress,
[Description("The Desired Task is Successfully Completed")] Completed
}
static class extensionClass {
public static string getDescription(this Enum e) {
Type eType = e.GetType();
string eName = Enum.GetName(eType, e);
if (eName != null) {
FieldInfo fieldInfo = eType.GetField(eName);
if (fieldInfo != null) {
DescriptionAttribute descriptionAttribute = Attribute.GetCustomAttribute(
fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (descriptionAttribute != null) {
return descriptionAttribute.Description;
}
}
}
return null;
}
}
class Program {
static void Main(string[] args) {
Status complete = Status.Completed;
string description = complete.getDescription();
Console.WriteLine(description);
}
}
}
输出:
The Desired Task is Successfully Completed
在上面的代码中,我们创建了一个扩展方法 getDescription
,该方法返回 C# 中的 Enum 值描述。该方法可以很好地工作,但是有点复杂。这种复杂性在下一节中得到了简化。
用 C# 中的 switch
语句将 Enum 转为字符串
通过使用 C# 中的 switch
语句,可以简化以前方法的许多复杂性。我们可以使用 C# 中的 switch
语句,为每个 Enum 值的字符串变量分配所需的值。请参见以下代码示例。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace enum_to_string {
public enum Status { InProgress, Completed }
static class extensionClass {
public static string getValue(this Status e) {
switch (e) {
case Status.InProgress:
return "The Desired Task is InProgress";
case Status.Completed:
return "The Desired Task is Successfully Completed";
}
return String.Empty;
}
}
class Program {
static void Main(string[] args) {
Status complete = Status.Completed;
string value = complete.getValue();
Console.WriteLine(value);
}
}
}
输出:
The Desired Task is Successfully Completed
在上面的代码中,我们创建了一个扩展方法 getValue()
,该方法使用 C# 中的 switch
语句返回基于 Enum 值的字符串。getValue()
函数使用 switch
语句,并为我们指定的 Enum 的每个值返回不同的字符串。
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径