private static Expression GenerateArrayOfKnownDimension(List <ParameterExpression> variables, ParameterExpression source, ParameterExpression clone, Type sourceType, Type elementType, ParameterExpression refTrackerParam) { var i = Expression.Parameter(typeof(int), "i"); var lengths = Expression.Parameter(typeof(int[]), "lengths"); var rank = sourceType.GetArrayRank(); variables.Add(i); variables.Add(lengths); List <Expression> notTrackedExpressions = new List <Expression>(); notTrackedExpressions.Add(Expression.IfThen(Expression.GreaterThanOrEqual(Expression.Constant(rank), Expression.Constant(255)), Expression.Throw(Expression.New(NotSupportedExceptionMih.ConstructorString(), Expression.Constant("Array with more than 255 dimensions are not supported"))))); notTrackedExpressions.Add(Expression.Assign(lengths, Expression.Call(CreateArrayMethodInfo.GetCreateArrayMethodInfo(typeof(int)), Expression.Constant(rank)))); notTrackedExpressions.AddRange(PopulateDimensionalArrayLength(source, i, lengths, rank)); notTrackedExpressions.Add(Expression.Assign(clone, Expression.Convert(Expression.Call(ArrayMih.CreateInstance(), Expression.Constant(elementType), lengths), sourceType))); notTrackedExpressions.Add(Expression.Call(refTrackerParam, ObjectClonerReferenceTrackerMih.Track(), source, clone)); notTrackedExpressions.AddRange(GenerateCopyDimensionalArray(source, clone, sourceType, variables, lengths, rank, refTrackerParam)); return(ObjectCloner.GenerateNullTrackedOrUntrackedExpression(source, clone, sourceType, refTrackerParam, Expression.Block(notTrackedExpressions))); }
/// <summary> /// Generate an expression tree to handle array serialization /// </summary> /// <param name="type">Type of the array</param> /// <param name="elementType">Type of the elements contained inside the array</param> /// <param name="outputStream">Stream that is written to</param> /// <param name="objToSerialize">Object to serialize</param> /// <param name="objTracking">Reference tracker</param> /// <returns>An expression tree to handle array serialization</returns> internal static Expression GenerateArrayOfKnownDimension(Type type, Type elementType, ParameterExpression outputStream, ParameterExpression objToSerialize, ParameterExpression objTracking) { List <Expression> notTrackedExpressions = new List <Expression>(); List <ParameterExpression> variables = new List <ParameterExpression>(); var i = Expression.Parameter(typeof(int), "i"); var lengths = Expression.Parameter(typeof(int[]), "lengths"); var trackedObjectPosition = Expression.Parameter(typeof(int?), "trackedObjectPosition"); var arr = Expression.Parameter(type, "arr"); var rank = type.GetArrayRank(); variables.Add(trackedObjectPosition); variables.Add(i); variables.Add(lengths); variables.Add(arr); notTrackedExpressions.Add(Expression.Call(objTracking, SerializerObjectTrackerMih.TrackObject(), objToSerialize)); notTrackedExpressions.Add(Expression.Assign(arr, Expression.Convert(objToSerialize, type))); notTrackedExpressions.Add(Expression.Assign(lengths, Expression.Call(CreateArrayMethodInfo.GetCreateArrayMethodInfo(typeof(int)), Expression.Constant(rank)))); notTrackedExpressions.Add(Expression.Assign(i, Expression.Constant(0))); notTrackedExpressions.Add(Expression.IfThen(Expression.GreaterThanOrEqual(Expression.Constant(rank), Expression.Constant(255)), Expression.Throw(Expression.New(NotSupportedExceptionMih.ConstructorString(), Expression.Constant("Array with more than 255 dimensions are not supported"))))); notTrackedExpressions.AddRange(WriteDimensionalArrayLength(outputStream, objTracking, i, arr, lengths, rank)); notTrackedExpressions.AddRange(WriteDimensionalArray(elementType, variables, outputStream, arr, rank, lengths, objTracking)); return(Serializer.GenerateNullTrackedOrUntrackedExpression(type, outputStream, objToSerialize, objTracking, notTrackedExpressions, variables)); }