using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace NXWMS.Code.Reflections { // /// 反射赋值 /// public static class ObjectReflection { public static PropertyInfo[] GetPropertyInfos(Type type) { return type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } /// /// 实体属性反射 /// /// 赋值对象 /// 被赋值对象 /// /// public static void AutoMapping(S s, T t) { PropertyInfo[] pps = GetPropertyInfos(s.GetType()); Type target = t.GetType(); foreach (var pp in pps) { PropertyInfo targetPP = target.GetProperty(pp.Name); object value = pp.GetValue(s, null); if (targetPP != null && value != null) { targetPP.SetValue(t, value, null); } } } /// /// 设置类中属性 /// /// /// /// /// /// public static string SetFieldValue(T t,string fieldName,object value) where T: class { try { var type = t.GetType(); var propertyInfo = type.GetProperty(fieldName); propertyInfo.SetValue(t, value, null); return string.Empty; } catch(Exception ex) { return ex.Message; } } /// /// 获取类中属性 /// /// /// /// public static string GetFieldValue(T t,string FieldName) where T : class { try { Type Ts = t.GetType(); object o = Ts.GetProperty(FieldName).GetValue(t, null); string Value = Convert.ToString(o); if (string.IsNullOrEmpty(Value)) return null; return Value; } catch { return null; } } } }