Visual Studio C# enum usage: Matching attributes is simply against the sky!
Visual Studio C# enum usage
If your colleagues know how to make good use of enum, please cherish it...
For a code that is easy to read and modify, it is a better practice to make good use of enums instead of pure numerical values! However, enum can only correspond to the application of numerical value. If you want to have more applications such as string, struct, class, etc., in general, you can only wait for death with both hands.
However, Visual Studio engineers will not disappoint, as much convenience as possible, so when using Visual Studio C#, you can get more information by attaching attributes to enum and using Reflection to make enum It no longer only corresponds to a certain value, but also corresponds to more items and information! Great Dot Net Csharp ah~
This is the general enum
public enum SampleEnum { None = 0, MyEnum01, MyEnum02, MyEnum03, };
This is the enum after adding the attribute
public enum SampleEnum { [System.ComponentModel.DescriptionAttribute("空的")] None, [System.ComponentModel.DescriptionAttribute("第一個")] MyEnum01, [System.ComponentModel.DescriptionAttribute("第二個")] MyEnum02, MyEnum03, };
In a general enum, it can only be achieved to make None = 0; MyEnum01 = 1; MyEnum02 = 2; MyEnum03 = 3, this state is just, if I want None; MyEnum01 ... can represent a corresponding string state, then I can pass Add attribute to enum to indicate.
To add an attribute to an enum, you only need to use square brackets above the enum to which the attribute is to be added to indicate that you want to add an attribute to an enum, and enter an attribute class in the square brackets, such as the above code is to use a system description attribute of . So now None, MyEnum01, MyEnum02 have attributes attached, while MyEnum03 has no attributes attached.
If the description attribute of the system cannot meet your needs, you can also inherit the attribute abstract class to create your own attribute!
The enum after adding the attribute I created is so beautiful I can't even open my eyes
public class SampleEnumAttribute : Attribute { private string _text = string.Empty; public SampleEnumAttribute(string text) { _text = text; } public string Text => _text; } /* 就可以將上面的"System.ComponentModel.DescriptionAttribute" 取代成"SampleEnumAttribute" */ public enum SampleEnum { [SampleEnumAttribute("空的")] None, [SampleEnumAttribute("第一個")] MyEnum01, [SampleEnumAttribute("第二個")] MyEnum02, MyEnum03, };
Then you can get the information in Attribute through Reflection:
※ The following is the writing method of the custom extension method. If you are not clear about this usage, you can refer to: Visual Studio C# extension methods make code better and life more complete
public static class SampleEnumExtension { public static string GetSampleEnumText(this SampleEnum sample) { System.Reflection.FieldInfo fi = sample.GetType().GetField(sample.ToString()); if (fi.GetCustomAttribute(typeof(SampleEnumAttribute), false) is SampleEnumAttribute attribute) { return attribute.Text; } else return null; } }
Combining the above-mentioned additional attributes (Attribute) method with reflection (Reflection), you can get the following results:
private static void Main(string[] args) { var text = string.Empty; Console.WriteLine("/// <output>"); text = SampleEnum.None.GetSampleEnumText(); Console.WriteLine($"/// {nameof(SampleEnum.None)} 的Attribute 訊息: {text}, {nameof(string.IsNullOrEmpty)}: {string.IsNullOrEmpty(text)}"); text = SampleEnum.MyEnum01.GetSampleEnumText(); Console.WriteLine($"/// {nameof(SampleEnum.MyEnum01)} 的Attribute 訊息: {text}, {nameof(string.IsNullOrEmpty)}: {string.IsNullOrEmpty(text)}"); text = SampleEnum.MyEnum02.GetSampleEnumText(); Console.WriteLine($"/// {nameof(SampleEnum.MyEnum02)} 的Attribute 訊息: {text}, {nameof(string.IsNullOrEmpty)}: {string.IsNullOrEmpty(text)}"); text = SampleEnum.MyEnum03.GetSampleEnumText(); Console.WriteLine($"/// {nameof(SampleEnum.MyEnum03)} 的Attribute 訊息: {text}, {nameof(string.IsNullOrEmpty)}: {string.IsNullOrEmpty(text)}"); Console.WriteLine("/// </output>"); Console.ReadLine(); } /// <output> /// None 的Attribute 訊息: 空的, IsNullOrEmpty: False /// MyEnum01 的Attribute 訊息: 第一個, IsNullOrEmpty: False /// MyEnum02 的Attribute 訊息: 第二個, IsNullOrEmpty: False /// MyEnum03 的Attribute 訊息: , IsNullOrEmpty: True /// </output>
You can get the string information of None, MyEnum01, MyEnum02, and because MyEnum03 has no Attribute attached, the returned string is null.
Original link Leo Studio
Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!