using System.Reflection; namespace CmsQueryExtensions.Extension { /// /// 类帮助 /// public class ClassHelper { /// /// 实体互转 /// /// 新转换的实体 /// 要转换的实体 /// /// public static T RotationMapping(S s) { T target = Activator.CreateInstance(); var originalObj = s.GetType(); var targetObj = typeof(T); foreach (PropertyInfo original in originalObj.GetProperties()) { foreach (PropertyInfo t in targetObj.GetProperties()) { if (t.Name == original.Name) { t.SetValue(target, original.GetValue(s, null), null); } } } return target; } /// /// 获取属性对象列表 /// /// /// /// /// /// public static PropertyInfo[] GetPropertyInfoList(T t) { var pros = typeof(T).GetProperties(); return pros; } /// /// 获取属性对象 /// /// /// /// /// /// public static PropertyInfo GetPropertyInfo(T t, string proName, out string errMsg) { errMsg = ""; var pro = typeof(T).GetProperty(proName); if (pro == null) { errMsg = "属性名'" + proName + "'不存在类'" + typeof(T).Name + "'中"; return null; } return pro; } /// /// 获取属性的值 /// /// /// /// /// /// public static string GetPropertyValue(T t, string proName, out string errMsg) { var pro = GetPropertyInfo(t, proName, out errMsg); if (!string.IsNullOrEmpty(errMsg)) { return string.Empty; } var pro_value = pro.GetValue(t, null); var str = pro_value == null ? string.Empty : Convert.ToString(pro_value); return str; } /// /// 获取属性的值 /// /// /// /// /// /// public static object GetPropertyValueForReObject(T t, string proName, out string errMsg) { var pro = GetPropertyInfo(t, proName, out errMsg); if (!string.IsNullOrEmpty(errMsg)) { return null; } var pro_value = pro.GetValue(t, null); var str = pro_value == null ? null : (pro_value); return str; } /// /// 通过属性对象获取属性的值 /// /// /// /// /// public static string GetPropertyValueByObject(T t, PropertyInfo pro) { var pro_value = pro.GetValue(t, null); var str = pro_value == null ? string.Empty : Convert.ToString(pro_value); return str; } /// /// 通过属性对象获取属性的值 /// /// /// /// /// public static object GetPropertyValue(T t, PropertyInfo pro) { var pro_value = pro.GetValue(t, null); return pro_value; } /// /// 获取特性【高级查询范围查询特性】 /// /// /// /// /// public static object[] GetHighSearchRangeAttributeByPro(PropertyInfo pro) { object[] Attributes = pro.GetCustomAttributes(typeof(HighSearchRangeAttribute), false); return Attributes; } /// /// 获取特性【不自动查询特性】 /// /// /// /// /// public static bool IsExistNoAutoQueryAttribute(PropertyInfo pro) { object[] attributes = pro.GetCustomAttributes(typeof(NoAutoQueryAttribute), false); if (attributes.Length > 0) { return true; } return false; } /// /// 获取属性的值 /// /// /// /// /// /// public static List GetPropertyValueForList(T t, string proName, out string errMsg) { var pro = GetPropertyInfo(t, proName, out errMsg); if (!string.IsNullOrEmpty(errMsg)) { return null; } var pro_value = pro.GetValue(t, null); var list = pro_value == null ? null : (List)pro_value; return list; } } }