
1 using System;
2 using System.ComponentModel.DataAnnotations;
3
4 public class LoginModel
5 {
6
7 [Display(Name = "用户名")]
8 public string UserName { get; set; }
9
10 public string ExternalLoginData { get; set; }
11 }
12 /// <summary>
13 /// 说明:对比实体类
14 /// 时间:2014年2月20日 14:17:48
15 /// Auhter:Kim
16 /// </summary>
17 /// <typeparam name="T"></typeparam>
18 /// <param name="newModel"></param>
19 /// <param name="oldModel"></param>
20 /// <returns></returns>
21 public string CompareModel<T>(T newModel, T oldModel) where T : class,new ()
22 {
23 StringBuilder strLog = new StringBuilder();
24 System.Reflection. PropertyInfo[] mPi = typeof(T).GetProperties();
25 for (int i = 0; i < mPi.Length; i++)
26 {
27 System.Reflection. PropertyInfo pi = mPi[i];
28 string strName = string .Empty;
29 string log=string .Empty;
30 if (pi.GetValue(oldModel, null ) != null && pi.GetValue(newModel, null ) != null)
31 {
32
33 strName = Attributes.GetDisplayInfo<T>(pi.Name);
34 string oldValue = pi.GetValue(oldModel, null).ToString();
35 string newValue = pi.GetValue(newModel, null).ToString();
36 if (oldValue != newValue)
37 {
38 if (GetNameByDictonary(strName, oldValue, newValue, out log))
39 strLog.Append(log);
40 else
41 strLog.AppendFormat("<strong>{0}</strong><span>{0} 从 {1} 改为 {2}</span> ", strName, oldValue, newValue);
42 }
43 }
44 }
45 return strLog.ToString();
46 }
47
48
49 /// <summary>
50 /// 获取DisplayInfo
51 /// </summary>
52 /// <param name="t"></param>
53 public static string GetDisplayInfo<T>( string name) where T : class,new ()
54 {
55 Type objType = typeof (T);
56 // Using reflection.
57 string strName = string .Empty;
58 PropertyInfo propInfo = objType.GetProperty(name);
59 object[] attrs = propInfo.GetCustomAttributes(typeof (DisplayAttribute), true); // Reflection.
60 // Displaying output.
61 if (attrs.Length > 0)
62 {
63 DisplayAttribute a = attrs[0] as DisplayAttribute;
64 strName = a.Name;
65 }
66
67 return strName;
68 }
