private void AddInclude(SimpleType source, MemberExpression memberSelector, LambdaExpression collectionInclude) { var members = source.GetAllMembers(); if (members.Count > 0 && memberSelector.GetDepth() > 1) { var innermostMember = ExpressionUtil.GetInnermostMemberExpression(memberSelector); foreach (var kvp in members) { if (kvp.Key == innermostMember.Member) { AddInclude(kvp.Value, ExpressionUtil.ReplaceInnermostMemberExpressionWithParameter(memberSelector) as MemberExpression, collectionInclude); return; } } } else { var parameter = ExpressionUtil.GetParameterExpression(memberSelector); if (collectionInclude != null) { source.Includes.Add(IncludeDirectiveUtil.GetIncludeInCollectionDirective(memberSelector, collectionInclude)); } else { source.Includes.Add(IncludeDirectiveUtil.GetIncludeDirective(memberSelector)); } } }
/// <summary> /// Finds whether exp1 is a part of the expression exp2 /// </summary> /// <param name="exp1"></param> /// <param name="exp2"></param> /// <returns></returns> public static bool IsPartOf(this MemberExpression exp1, MemberExpression exp2) { Func <MemberExpression, int, MemberExpression> SliceToDepth = (exp, depth) => { var currentDepth = exp.GetDepth(); for (int i = currentDepth; i > depth; i--) { exp = exp.Expression as MemberExpression; } return(exp); }; var depth1 = exp1.GetDepth(); var depth2 = exp2.GetDepth(); if (depth1 >= depth2) { return(false); } var partOfExp2 = SliceToDepth(exp2, depth1); return(IsMatching(exp1, partOfExp2)); }
private void AddIncludes(SerializableObjectGraph objectGraph, MemberExpression includedPropertyChain, IEnumerable <MemberExpression> collectionIncludes) { var memberExp = ExpressionUtil.GetInnermostMemberExpression(includedPropertyChain); var propInfo = objectGraph.GetInstance().GetType().GetProperty(memberExp.Member.Name); if (IsPropertyLoadedOnEntity(objectGraph.GetInstance(), propInfo)) { var propVal = propInfo.GetValue(objectGraph.GetInstance(), null); if (propVal == null) { return; } if (TypesUtil.IsNonPrimitiveCollection(propInfo.PropertyType)) { var collInclude = objectGraph.IncludedMembers.ContainsKey(propInfo) ? objectGraph.IncludedMembers[propInfo] : null; if (collInclude == null) { var list = new List <IObjectGraph>(); foreach (var item in (propVal as IEnumerable)) { list.Add(new SerializableInstance { Object = item, InstanceType = item.GetType() }); } collInclude = new SerializableCollection { Collection = list, InstanceType = propInfo.PropertyType }; objectGraph.IncludedMembers.Add(propInfo, collInclude); } if (collectionIncludes != null) { foreach (SerializableObjectGraph instanceWrapper in (collInclude as SerializableCollection).Collection) { foreach (var include in collectionIncludes) { AddIncludes(instanceWrapper, include, null); } } } } else { var fieldInclude = objectGraph.IncludedMembers.ContainsKey(propInfo) ? objectGraph.IncludedMembers[propInfo] : null; if (fieldInclude == null) { fieldInclude = new SerializableInstance { Object = propVal, InstanceType = propInfo.PropertyType }; objectGraph.IncludedMembers.Add(propInfo, fieldInclude); } if (includedPropertyChain.GetDepth() > 1) { AddIncludes(fieldInclude, ExpressionUtil.ReplaceInnermostMemberExpression(includedPropertyChain, Expression.Parameter(memberExp.Type, "x")) as MemberExpression, collectionIncludes); } } } }