您的位置:首页 > 其它

.net 自动打印枚举值 (可以兼容16进制的枚举值)

2016-03-25 10:55 309 查看
相关背景:产品中有大量的枚举值,而且相当一部分是16进制的定义,但是我们对应的 DB中存储的对应的是10进制的数值。

一个一个来算,比较费时,而且相当麻烦。因此,有了下面这个工具,可以批量瞬间生成某个枚举对应的项与值(10进制的数值)。非常方便,节省时间。

//using System;

using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Data;
//using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
//using System.Windows.Forms;

//using System.Collections.Generic;
//using System.Text;

public class Test
{
public static void Main()
{
//string str;
//str="hi,您好吧";
//Console.WriteLine(str);

//string s = PrintEnumValue2<BodyExerciseStrength>();
//Console.WriteLine(s);

PrintEnumValue2<BodyExerciseStrength>();

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public enum BodyExerciseStrength
{
/// <summary>
/// The unknown
/// </summary>

Unknown = 0,
/// <summary>
/// The slight
/// </summary>

Light = 0x10,
/// <summary>
/// The medium
/// </summary>

Medium = 0x20,
/// <summary>
/// The heavy
/// </summary>
Heavy = 0x30

}

public enum AppPlatform
{

/// <summary>
/// 未知
/// </summary>

none = 0,

/// <summary>
/// ANDROID
/// </summary>

ANDROID,

/// <summary>
/// IOS
/// </summary>

IOS

}

////////////////////////////////////////////////////////////////////////////////////////////////////////

 public static string PrintEnumValue2<T>()
       {
           #region ### test enum values

           Type item = typeof(T);
           Array Arrays_name = Enum.GetNames(item);
           Array Arrays = Enum.GetValues(item);

           StringBuilder sb = new StringBuilder();
           
           string sTitle= item.ToString();
           int iPos = sTitle.IndexOf("+");
 
           sTitle =  sTitle.Substring(iPos + 1);
     
           sb.AppendLine("//==" + sTitle + "==");
           for (int i = 0; i < Arrays.LongLength; i++)
           {

               sb.Append("  " + Arrays_name.GetValue(i) + ":");
               sb.Append((int)Arrays.GetValue(i));
               sb.AppendLine();
           }

           string sResult = sb.ToString();

           Console.WriteLine(sResult);

           #endregion

           return sResult;
       }

 ////////////////////////////////////////////////////////////////////////////////////////////////////////

}


运行结果:

//BodyExerciseStrength:

  Unknown:0

  Light:16

  Medium:32

  Heavy:48

 源代码: http://www.mcqyy.com/RunCode/csharp/#id/aaed2e056e4823c39c9b4e69105cef83
(结束)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: