利用反射获取静态类的静态属性值(已知属性名称的字符串表示)

列如获取Phone属性的值

typeof(ExcelColumnName).GetProperty(“Phone”).GetValue(null, null)//ExcelColumnName是静态类

 ExcelColumnName类如下:

 1 /// 
 2         /// 要上传的excel所包含的列
 3         /// 
 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 = "手机号(必填)";

  

转载于:https://www.cnblogs.com/dansediao/p/5774393.html

你可能感兴趣的:(利用反射获取静态类的静态属性值(已知属性名称的字符串表示))