C#:通过反射获得指定类的常亮名称和值

摘要:
1、需求场景:我需要把常量值转成属性名输出,需要用反射把相关的变量用Json或者DataSet结构化出来
2、困难点:
(1)怎么识别有效的变量,就单单获得常量,静态变量和普通变量忽略
(2)怎么获得对应的值
(3)类有继承关系,怎么把父类的变量也给输出

原文链接:
http://www.lookdaima.com/page/docItemDetail.html?id=84347696-7f1c-4bd4-ba7d-5d4429f87a48

界面效果:

 

 

相关代码:

  1 using eKing.LdmWebUtil.Names;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Data;
  5 using System.Reflection;
  6 
  7 public partial class WebForms_Adms_Blanks__Adms_Tools_LdmNameToCode_Default 
  8     : System.Web.UI.Page
  9 {
 10 
 11     /// <summary>
 12     /// 
 13     /// </summary>
 14     [Serializable]
 15     public class InfoData
 16     {
 17         public InfoData()
 18         {
 19         }
 20 
 21         #region TheName ~ 名称
 22 
 23         /// <summary>
 24         /// TheName ~ 名称
 25         /// </summary>
 26         protected string m_TheName = "";
 27 
 28         /// <summary>
 29         /// TheName ~ 名称
 30         /// </summary>
 31         public string TheName
 32         {
 33             get
 34             {
 35                 return m_TheName;
 36             }
 37             set
 38             {
 39                 m_TheName = value;
 40             }
 41         }
 42 
 43         #endregion TheName ~ 名称
 44 
 45 
 46         #region TheValue ~ 值
 47 
 48         /// <summary>
 49         /// TheValue ~ 值
 50         /// </summary>
 51         protected string m_TheValue = "";
 52 
 53         /// <summary>
 54         /// TheValue ~ 值
 55         /// </summary>
 56         public string TheValue
 57         {
 58             get
 59             {
 60                 return m_TheValue;
 61             }
 62             set
 63             {
 64                 m_TheValue = value;
 65             }
 66         }
 67 
 68         #endregion TheValue ~ 值
 69 
 70         #region ExceptionText ~ 异常提示
 71 
 72         /// <summary>
 73         /// ExceptionText ~ 异常提示
 74         /// </summary>
 75         protected string m_ExceptionText = "";
 76 
 77         /// <summary>
 78         /// ExceptionText ~ 异常提示
 79         /// </summary>
 80         public string ExceptionText
 81         {
 82             get
 83             {
 84                 return m_ExceptionText;
 85             }
 86             set
 87             {
 88                 m_ExceptionText = value;
 89             }
 90         }
 91 
 92         #endregion ExceptionText ~ 异常提示
 93 
 94     }
 95 
 96     /// <summary>
 97     /// 
 98     /// </summary>
 99     /// <param name="theList"></param>
100     /// <param name="theName"></param>
101     /// <returns></returns>
102     public InfoData InfoDataFind(List<InfoData> theList, string theName)
103     {
104         foreach (InfoData item in theList)
105         {
106             if (item.TheName == theName)
107                 return item;
108         }
109 
110         return null;
111     }
112 
113     /// <summary>
114     /// 通过反射获得所有Field的常用字段
115     /// </summary>
116     /// <param name="theList"></param>
117     /// <param name="thisType"></param>
118     protected void ToResultByType(List<InfoData> theList, Type thisType)
119     {
120 
121         FieldInfo[] fA = thisType.GetFields();
122 
123         
124         Type typeString = typeof(string);
125         InfoData infoDataV = null;
126         object oValue = null;
127 
128         foreach (FieldInfo fi in fA)
129         {
130             // 不是字符串类型 //
131             if (fi.FieldType != typeString)
132                 continue;
133 
134             // 不是常量 //
135             if (!fi.IsStatic)
136                 continue;
137 
138             if (!fi.IsLiteral)
139                 continue;
140 
141             infoDataV = InfoDataFind(theList, fi.Name);
142 
143             if (infoDataV != null)
144                 continue;
145 
146             infoDataV = new InfoData();
147             infoDataV.TheName = fi.Name;
148 
149             try
150             {
151                 oValue = fi.GetValue(null);
152 
153                 if (oValue == null)
154                     infoDataV.TheValue = null;
155                 else
156                     infoDataV.TheValue = oValue.ToString();
157             }
158             catch(Exception err) 
159             {
160                 infoDataV.ExceptionText = err.Message; 
161             }
162 
163             theList.Add(infoDataV);
164         }
165 
166 
167     }
168 
169     /// <summary>
170     /// 通过反射获得逻辑类的常用值
171     /// </summary>
172     /// <returns></returns>
173     protected string ToResult(string outputType)
174     {
175         Type thisType = typeof(LdmName);
176         List<InfoData> theList = new List<InfoData>();
177 
178         while (true)
179         {
180             if (thisType == null)
181                 break;
182 
183             ToResultByType(theList, thisType);
184 
185             thisType = thisType.BaseType;
186         }
187 
188 
189         if (outputType == "json")
190         {
191             return Newtonsoft.Json.JsonConvert.SerializeObject(theList);
192         }
193 
194         DataSet ds = new DataSet();
195         DataTable dt = new DataTable();
196 
197         ds.Tables.Add(dt);
198 
199         dt.Columns.Add(new DataColumn("TheName"));
200         dt.Columns.Add(new DataColumn("TheValue"));
201         dt.Columns.Add(new DataColumn("ExceptionText"));
202 
203         DataRow dr = null;
204 
205         foreach (InfoData s in theList)
206         {
207             dr = dt.NewRow();
208             dt.Rows.Add(dr);
209 
210             dr["TheName"] = s.TheName;
211             dr["TheValue"] = s.TheValue;
212             dr["ExceptionText"] = s.ExceptionText;
213         }
214 
215         return ds.GetXml();
216     }
217 
218     protected void Page_Load(object sender, EventArgs e)
219     {
220         
221     }
222 
223     protected void btn_DataSet_Click(object sender, EventArgs e)
224     {
225         txt_Result.Text = ToResult("ds");
226     }
227 
228     protected void btn_Json_Click(object sender, EventArgs e)
229     {
230         txt_Result.Text = ToResult("json");
231     }
232 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using eKing.EkWeb.Names;
 5 
 6 namespace eKing.LdmWebUtil.Names
 7 {
 8     /// <summary>
 9     /// 
10     /// </summary>
11     public class LdmName
12         :
13         EkWebConstName
14     {
15         public LdmName()
16         {
17             //
18             //TODO: 在此处添加构造函数逻辑
19             // 
20         
21         
22         }
23 
24         /// <summary>
25         /// 
26         /// </summary>
27         public string one = "one";
28 
29         /// <summary>
30         /// 
31         /// </summary>
32         public static string two = "two";
33 
34         /// <summary>
35         /// TheText
36         /// </summary>
37         public const string TheText = "TheText";
38 
39         /// <summary>
40         /// TheDescription
41         /// </summary>
42         public const string TheDescription = "TheDescription";
43 
44         
45 
46     }
47 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 
 4 using System.Text;
 5 
 6 
 7 namespace eKing.EkWeb.Names
 8 {
 9     /// <summary>
10     /// 
11     /// </summary>
12     public class EkWebConstName
13     {
14         /// <summary>
15         /// 
16         /// </summary>
17         public EkWebConstName()
18         {
19 
20         }
21 
22         #region PmSlnSort_Id ~ 应用分类
23 
24         /// <summary>
25         /// PmSlnSort_Id ~ 应用分类
26         /// </summary>
27         public const string PmSlnSort_Id = "PmSlnSort_Id";
28 
29         #endregion PmSlnSort_Id ~ 应用分类
30 
31 
32         #region PmBakData_Id ~ 备份记录
33 
34         /// <summary>
35         /// PmBakData_Id ~ 备份记录
36         /// </summary>
37         public const string PmBakData_Id = "PmBakData_Id";
38 
39         #endregion PmBakData_Id ~ 备份记录
40 
41 
42         #region CommentShowFlag ~ 显示注释
43 
44         /// <summary>
45         /// CommentShowFlag ~ 显示注释
46         /// </summary>
47         public const string CommentShowFlag = "CommentShowFlag";
48 
49         #endregion CommentShowFlag ~ 显示注释
50  
51 
52     }
53 
54 }
View Code

 

posted @ 2021-02-07 13:39  看代码`lookdaima.com  阅读(118)  评论(0编辑  收藏  举报