4 Star 4 Fork 1

小王子1987 / Log日志类库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EnumManager.cs 4.99 KB
一键复制 编辑 原始数据 按行查看 历史
小王子1987 提交于 2017-05-18 16:22 . 全部代码文件,平台VS2010
using System;
using System.Configuration;
using System.Data;
using System.Reflection;
using CommonLib.Log;
namespace CommonLib.Log
{
/// <summary>
/// 文本日志类型
/// </summary>
public enum LogType
{
[Text("错误")]
E = 0,
[Text("DB类")]
SQL = 1,
[Text("提示")]
I = 2,
[Text("警告")]
A = 3
}
/// <summary>
/// 消息状态
/// </summary>
public enum MessageStatus
{
[Text("未处理")]
UnDo = 0,
[Text("已处理")]
Done = 1
}
/// <summary>
/// 反馈结果/意见
/// </summary>
public enum FBResult
{
[Text("NG")]
NG = 0,
[Text("OK")]
OK = 1
}
public class EnumManager
{
public static string GetEnumTextVal(int enumConst, Type enumType)
{
string textVal = string.Empty;
try
{
if (!enumType.IsEnum)
{
throw new InvalidOperationException();
}
Type typeDescription = typeof(TextAttribute);
FieldInfo fieldInfo = enumType.GetField(Enum.GetName(enumType, enumConst).ToString());
if (fieldInfo != null)
{
object[] arr = fieldInfo.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
TextAttribute textAttribute = (TextAttribute)arr[0];
textVal = textAttribute.Text;
}
}
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTextVal => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
}
return textVal;
}
public static DataTable GetEnumTable(Type enumType)
{
DataTable dt = new DataTable();
dt.Columns.Add("Text", typeof(System.String));
dt.Columns.Add("Value", typeof(System.String));
try
{
if (!enumType.IsEnum)
{
throw new InvalidOperationException();
}
Type typeDescription = typeof(TextAttribute);
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum == true)
{
DataRow dr = dt.NewRow();
dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
TextAttribute aa = (TextAttribute)arr[0];
dr["Text"] = aa.Text;
}
else
{
dr["Text"] = field.Name;
}
dt.Rows.Add(dr);
}
}
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTable => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
}
return dt;
}
public static DataTable GetEnumTableForText(Type enumType, string text)
{
DataTable dt = new DataTable();
dt.Columns.Add("Text", typeof(System.String));
dt.Columns.Add("Value", typeof(System.String));
try
{
if (!enumType.IsEnum)
{
throw new InvalidOperationException();
}
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum == true && enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString() == text)
{
DataRow dr = dt.NewRow();
dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
dr["Text"] = enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString();
dt.Rows.Add(dr);
}
}
}
catch (Exception ex)
{
Logger.getInstance().WriteToLog((int)LogType.E, "GetEnumTableForText => " + ex.Message, ConfigurationManager.AppSettings["CommonLog"].ToString());
}
return dt;
}
}
}
C#
1
https://gitee.com/xiaowangzi_1987/logrizhileiku.git
git@gitee.com:xiaowangzi_1987/logrizhileiku.git
xiaowangzi_1987
logrizhileiku
Log日志类库
master

搜索帮助