代码拉取完成,页面将自动刷新
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;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。