利用反射獲取靜態類的靜態屬性值(已知屬性名稱的字符串表示)


列如獲取Phone屬性的值

typeof(ExcelColumnName).GetProperty(“Phone”).GetValue(null, null)//ExcelColumnName是靜態類

 ExcelColumnName類如下:

 1 /// <summary>
 2         /// 要上傳的excel所包含的列
 3         /// </summary>
 4         public static class ExcelColumnName
 5         {
 6             public static string Phone { get; set; } = "手機號(必填)";
 7             public static string Name { get; set; } = "用戶名(默認與手機號相同)";
 8             public static string RealName { get; set; } = "真實姓名";
 9             public static string CardNo { get; set; } = "身份證號";
10             public static string Referees { get; set; } = "推薦人";
11             public static string CreateTime { get; set; } = "報名日期(格式2016/1/2)";
12             public static string CompanyName { get; set; } = "工作單位";
13             public static string Email { get; set; } = "郵箱";
14 
15             public static List<string> ColumnList = new List<string>
16             {
17                 Phone,
18                 Name,
19                 RealName,
20                 CardNo,
21                 Referees,
22                 CreateTime,
23                 CompanyName,
24                 Email
25             };
26         }

 Ps:要特別注意的是靜態字段如果是以下這樣定義的話就獲取不到了,原因是:這樣的話Phone就不被認為是類的屬性了,再通過反射取屬性就去不到了,根本找不到這個屬性

public static string Phone = "手機號(必填)";

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM