public static RuntimePropertyInfo GetPropertyInfo(MemberExpression selector) { MemberExpression me = selector; if (me == null) { throw new NotSupportedException("传入表达式类型不正确"); } Stack <RuntimePropertyInfo> stack = new Stack <RuntimePropertyInfo>(); stack.Push(new RuntimePropertyInfo(me.Member as PropertyInfo)); MemberExpression pe = me.Expression as MemberExpression; while (pe != null && pe.NodeType == ExpressionType.MemberAccess) { stack.Push(new RuntimePropertyInfo(pe.Member as PropertyInfo)); pe = pe.Expression as MemberExpression; } RuntimePropertyInfo cur = stack.Pop(); RuntimePropertyInfo p = null; while (stack.Any()) { p = stack.Pop(); p.ParentProperty = cur; cur = p; } return(cur); }
public void Remove(RuntimePropertyInfo propertyInfo) { if (propertyInfo != null) { propertyList.Remove(propertyInfo); } }
public void Add(RuntimePropertyInfo propertyInfo) { if (propertyInfo != null) { propertyList.Add(propertyInfo); } }
public void Remove <TMember>(Expression <Func <TEntity, TMember> > field) { if (field != null) { RuntimePropertyInfo pi = getPropertyInfo(field); propertyList.Remove(pi); } }
private RuntimePropertyInfo getPropertyInfo <TMember>(Expression <Func <TEntity, TMember> > selector) { return(RuntimePropertyInfo.GetPropertyInfo(selector)); }
/// <summary> /// 递归检查符合条件的属性 /// </summary> /// <param name="entityType">实例类型</param> /// <param name="obj">实例</param> /// <param name="checkProc">检查委托</param> /// <param name="collection">属性集合</param> /// <param name="parentProperty">上级属性</param> /// <param name="refObjects">已检查的实例列表,防止无限递归</param> private static void getProperties <T>(Type entityType, object obj, Func <RuntimePropertyInfo, object, bool> checkProc, PropertyCollection <T> collection, RuntimePropertyInfo parentProperty, List <object> refObjects) where T : class, new() { refObjects.Add(obj); PropertyInfo[] pis = entityType.GetProperties(); Func <RuntimePropertyInfo, object, bool> actualCheckProc = checkProc; if (actualCheckProc == null) { actualCheckProc = (p, o) => { return(true); }; } bool isPass = false; foreach (PropertyInfo pi in pis) { if (!pi.CanRead) { continue; } object val = null; if (obj != null) { val = pi.GetValue(obj, null); } RuntimePropertyInfo curProperty = new RuntimePropertyInfo(pi); curProperty.ParentProperty = parentProperty; isPass = actualCheckProc(curProperty, val); if (isPass) { collection.Add(curProperty); } //递归子级对象 if (pi.PropertyType.IsClass && val != null && !refObjects.Any(x => x == val) ) { bool goOn = true; foreach (Type t in ignoreTypes) { if (pi.PropertyType == t || pi.PropertyType.IsSubclassOf(t)) { goOn = false; break; } if (t.IsInterface && t.IsAssignableFrom(pi.PropertyType)) { goOn = false; break; } } if (goOn) { getProperties(pi.PropertyType, val, actualCheckProc, collection, curProperty, refObjects); } } } }