通过反射获得枚举的对应值显示

2017-06-15 17:13:23  访问(1602) 赞(0) 踩(0)

  • using System;
    using System.IO;
    using System.Reflection;
    using System.Text;
    
    namespace eKingWGSS.Website.Management.WebForms.Tools.DevEnum
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theResult"></param>
            /// <param name="isEnd"></param>
            protected void PrintHtmlTable(StringBuilder theResult,bool isEnd)
            {
                if (isEnd)
                    theResult.AppendLine("</table>");
                else
                    theResult.AppendLine("<table border=\"1\">");
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theResult"></param>
            /// <param name="isEnd"></param>
            protected void PrintHtmlTr(StringBuilder theResult, bool isEnd)
            {
                if (isEnd)
                    theResult.AppendLine("</tr>");
                else
                    theResult.AppendLine("<tr>");
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theResult"></param>
            /// <param name="strLeft"></param>
            /// <param name="leftStyle"></param>
            /// <param name="strRight"></param>
            /// <param name="rightStyle"></param>
            protected void PrintHtmlTd(StringBuilder theResult, string strLeft,string leftStyle, string strRight, string rightStyle)
            {
                theResult.AppendLine("<td " + leftStyle + ">" + strLeft + "</td>");
                theResult.AppendLine("<td "+ rightStyle + ">" + strRight + "</td>");
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theResult"></param>
            /// <param name="strLeft"></param>
            /// <param name="strRight"></param>
            protected void PrintHtmlTd(StringBuilder theResult, string strLeft,string strRight)
            {
                theResult.AppendLine("<td>" + strLeft + "</td>");
                theResult.AppendLine("<td>" + strRight + "</td>");
            }
    
    
            /// <summary>  
            /// 获得枚举Name的数组  
            /// </summary>  
            /// <param name="t"></param>  
            /// <returns></returns>  
            protected string[] GetEnumNames(Type t)
            {
                return System.Enum.GetNames(t);
            }
    
            /// <summary>  
            /// 获得枚举值的Int[]  
            /// </summary>  
            /// <param name="t"></param>  
            /// <returns></returns>  
            protected int[] GetEnumIntArray(Type t)
            {
                return System.Enum.GetValues(t) as int[];
            }
    
            /// <summary>
            /// 获得除去后缀后的仅文件名
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public string FileOnlyNameGet(string fileName)
            {
                if (fileName == null || fileName.Length == 0)
                    return "";
    
                int idxOne = fileName.LastIndexOf('/');
                int idxTwo = fileName.LastIndexOf('\\');
    
                if (idxOne != -1)
                {
                    if (idxTwo > idxOne)
                        fileName = fileName.Substring(idxTwo + 1);
                    else
                        fileName = fileName.Substring(idxOne + 1);
                }
                else
                {
                    if (idxTwo != -1)
                        fileName = fileName.Substring(idxTwo + 1);
                }
    
                int idx = fileName.LastIndexOf('.');
    
                if (idx != -1)
                    fileName = fileName.Substring(0, idx);
    
                return fileName;
            }
    
    
            /// <summary>
            /// 获得DLL的路径
            /// </summary>
            /// <param name="strClassName"></param>
            /// <returns></returns>
            protected string GetDLLName(string strClassName)
            {
                if (strClassName == null)
                    return "";
    
                strClassName = strClassName.Trim().ToLower();
    
                DirectoryInfo dir = new DirectoryInfo
                    (Request.PhysicalApplicationPath + "bin");
    
                FileInfo[] fA = dir.GetFiles();
    
                if (fA == null)
                    return "";
    
    
                string strEx = null;
                string strName = null;
                foreach (FileInfo fi in fA)
                {
                    if (fi.Extension == null)
                        continue;
    
                    strEx = fi.Extension.Trim().ToLower();
    
                    if (!(strEx == ".dll" || strEx == ".exe"))
                        continue;
    
                    strName = FileOnlyNameGet(fi.Name);
    
                    if (strClassName.StartsWith(strName.ToLower() + "."))
                    {
                        return fi.FullName;
                    }
                }
    
                return "";
            }
    
           
        }
    }
    
  • <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="eKingWGSS.Website.Management.WebForms.Tools.DevEnum.Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
               <a href="Default.aspx">刷新</a>
            <br />
            枚举逻辑类:<asp:TextBox ID="txt_EnumClassName" runat="server" Width="400px"></asp:TextBox>
            <asp:Button ID="btn_Read" runat="server" Text="读取" OnClick="btn_Read_Click" />
            <br />
            (参考输入:eKing.SzdfLib.Enums.Education+EmEducation)
            <br />
            <br />
            <asp:Literal runat="server" ID="lt_Result"></asp:Literal>
        </div>
        </form>
    </body>
    </html>
    
    
  •  /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btn_Read_Click(object sender, EventArgs e)
            {
                try {
    
                   
                    string strClassName = txt_EnumClassName.Text.Trim();
    
                    if(strClassName.Length == 0)
                    {
                        lt_Result.Text = "请输入枚举类";
                        return;
                    }
    
                    // 通过className和bin目录获得对应的dll名称 //
                    string dllFullName = GetDLLName(strClassName);
    
                    if (dllFullName.Length == 0)
                    {
                        lt_Result.Text = "没有找到枚举对应所在的dll";
                        return;
                    }
    
                    Assembly ass = null;
                    ass = Assembly.LoadFile(dllFullName);
    
                    if(ass == null)
                    {
                        lt_Result.Text = "没有找到dll对应的Assembly";
                        return;
                    }
    
                    Type enumType
                        = ass.GetType(strClassName, false, true);
    
                    
                    //if (enumType == null)
                    //{
                    //    string lowerClassName = strClassName.ToLower();
                    //    Type[] tA = ass.GetTypes();
    
                    //    foreach (Type t in tA)
                    //    {
                    //        if (t.FullName.ToLower() == lowerClassName)
                    //        {
                    //            enumType = t;
                    //            break;
                    //        }
                    //    }
                    //}
    
    
    
                    if (enumType == null)
                    {
                        lt_Result.Text = "没有找到对应的Type";
                        return;
                    }
    
                    StringBuilder theResult = new StringBuilder();
    
                    PrintHtmlTable(theResult, false);
    
               
                    int[] vA = GetEnumIntArray(enumType);
                    string[] sA = GetEnumNames(enumType);
    
                    int vLen = vA.Length;
                    int sLen = sA.Length;
    
                    if(vLen!=sLen)
                    {
                        lt_Result.Text = "枚举值不匹配";
                        return;
                    }
    
                    for (int i = 0; i < vLen; ++i)
                    {
                        PrintHtmlTr(theResult, false);
    
                        PrintHtmlTd(theResult, sA[i], vA[i].ToString());
    
                        PrintHtmlTr(theResult, true);
                    } 
    
                    PrintHtmlTable(theResult, true);
    
                    lt_Result.Text = theResult.ToString();
                }
                catch (Exception err)
                {
                    lt_Result.Text = err.Message;
                }
            }
    

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)