private static Expression SerializeISerializable(Type type, List <ParameterExpression> variables, ParameterExpression outputStream, ParameterExpression objToSerialize, ParameterExpression objTracking) { var fc = Expression.Parameter(typeof(FormatterConverter), "fc"); var context = Expression.Parameter(typeof(StreamingContext), "context"); var si = Expression.Parameter(typeof(SerializationInfo), "si"); var iser = Expression.Parameter(typeof(ISerializable), "iser"); variables.Add(fc); variables.Add(context); variables.Add(si); variables.Add(iser); var getEnumeratorMethodInfo = SerializationInfoMih.GetEnumerator(); var enumeratorMethod = Expression.Call(si, getEnumeratorMethodInfo); var loopBodyCargo = new EnumerableLoopBodyCargo(); loopBodyCargo.EnumeratorType = typeof(SerializationInfoEnumerator); loopBodyCargo.KvpType = typeof(SerializationEntry); var preLoopActions = new List <Expression>(); preLoopActions.Add(PrimitiveHelpers.WriteInt32(outputStream, Expression.Property(si, SerializationInfoMih.MemberCount()), objTracking)); var notTrackedExpressions = new List <Expression>(); notTrackedExpressions.Add(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0, typeof(byte)), objTracking)); notTrackedExpressions.Add(Expression.Assign(fc, Expression.New(typeof(FormatterConverter)))); notTrackedExpressions.Add(Expression.Assign(context, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); notTrackedExpressions.Add(Expression.Assign(si, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(type), fc))); notTrackedExpressions.Add(Expression.Assign(iser, Expression.Convert(objToSerialize, typeof(ISerializable)))); notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializingAttributeExpression(type, objToSerialize, context)); notTrackedExpressions.Add(Expression.Call(iser, ISerializableMih.GetObjectData(), si, context)); notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializedAttributeExpression(type, objToSerialize, context)); notTrackedExpressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(si, "IsFullTypeNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the full type name for an ISerializable is not supported"))))); notTrackedExpressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(si, "IsAssemblyNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the assembly name for an ISerializable is not supported"))))); notTrackedExpressions.Add(EnumerableLoopHelper.GenerateEnumeratorLoop(variables, DictionarySerializer.GetKeyValuePairLoopBodyCargo(outputStream, objTracking, "Name", typeof(string), "Value", typeof(object)), enumeratorMethod, preLoopActions, loopBodyCargo)); notTrackedExpressions.Add(Expression.Call(objTracking, SerializerObjectTrackerMih.TrackObject(), objToSerialize)); return(Expression.Block(notTrackedExpressions)); }
private static Func <EnumerableLoopBodyCargo, Expression> GetLoopBodyCargo(ParameterExpression siClone, ParameterExpression clonedItem, ParameterExpression refTrackerParam) { Func <EnumerableLoopBodyCargo, Expression> loopBody = cargo => { var keyExpression = Expression.Property(Expression.Property(cargo.Enumerator, cargo.EnumeratorType.GetProperty("Current")), cargo.KvpType.GetProperty("Name")); var valueExpression = Expression.Property(Expression.Property(cargo.Enumerator, cargo.EnumeratorType.GetProperty("Current")), cargo.KvpType.GetProperty("Value")); var cloneValueExpr = Expression.Block(ClassCloner.GetCloneClassTypeExpression(refTrackerParam, valueExpression, clonedItem, typeof(object)), Expression.Call(siClone, SerializationInfoMih.AddValue(), keyExpression, clonedItem)); return(Expression.IfThenElse(Expression.Equal(valueExpression, Expression.Constant(null)), Expression.Call(siClone, SerializationInfoMih.AddValue(), keyExpression, Expression.Constant((object)null, typeof(object))), cloneValueExpr)); }; return(loopBody); }
private static Expression DeserializeISerializable(Type type, List <ParameterExpression> variables, ParameterExpression inputStream, ParameterExpression objTracker, ParameterExpression newInstance) { var length = Expression.Parameter(typeof(int), "length"); var i = Expression.Parameter(typeof(int), "i"); var key = Expression.Parameter(typeof(string), "key"); var value = Expression.Parameter(typeof(object), "value"); var typeName = Expression.Parameter(typeof(string), "typeName"); var typeExpr = Expression.Parameter(typeof(TypeWithHashCode), "type"); var deserializer = Expression.Parameter(typeof(Func <Stream, DeserializerObjectTracker, object>), "deserializer"); var typeHashCode = Expression.Parameter(typeof(int), "typeHashCode"); var fc = Expression.Parameter(typeof(FormatterConverter), "fc"); var context = Expression.Parameter(typeof(StreamingContext), "context"); var si = Expression.Parameter(typeof(SerializationInfo), "si"); variables.Add(fc); variables.Add(context); variables.Add(si); variables.Add(key); variables.Add(value); variables.Add(length); variables.Add(i); variables.Add(deserializer); variables.Add(typeName); variables.Add(typeExpr); variables.Add(typeHashCode); var loopExpressions = new List <Expression>(); DictionaryDeserializer.GetReadExpression(inputStream, objTracker, typeof(string), loopExpressions, key, typeExpr, typeName, typeHashCode, deserializer); DictionaryDeserializer.GetReadExpression(inputStream, objTracker, typeof(object), loopExpressions, value, typeExpr, typeName, typeHashCode, deserializer); loopExpressions.Add(Expression.Call(si, SerializationInfoMih.AddValue(), key, value)); loopExpressions.Add(Expression.Assign(i, Expression.Add(i, Expression.Constant(1)))); var cond = Expression.LessThan(i, length); var loopBody = Expression.Block(loopExpressions); var breakLabel = Expression.Label("breakLabel"); var loop = Expression.Loop(Expression.IfThenElse(cond, loopBody, Expression.Break(breakLabel)), breakLabel); var notTrackedExpressions = new List <Expression>(); notTrackedExpressions.Add(Expression.Assign(fc, Expression.New(typeof(FormatterConverter)))); notTrackedExpressions.Add(Expression.Assign(context, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); notTrackedExpressions.Add(Expression.Assign(si, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(type), fc))); notTrackedExpressions.Add(Expression.Assign(length, PrimitiveHelpers.ReadInt32(inputStream, objTracker))); notTrackedExpressions.Add(Expression.Assign(i, Expression.Constant(0))); notTrackedExpressions.Add(loop); notTrackedExpressions.Add(Expression.Assign(newInstance, Expression.New(ISerializableSerializer.GetSerializationConstructor(type), si, context))); if (type.IsClass) { notTrackedExpressions.Add(Expression.Call(objTracker, DeserializerObjectTrackerMih.TrackedObject(), newInstance)); } notTrackedExpressions.AddRange(SerializationCallbacksHelper.GenerateOnDeserializedAttributeExpression(type, newInstance, context)); notTrackedExpressions.Add(SerializationCallbacksHelper.GenerateCallIDeserializationExpression(type, newInstance)); notTrackedExpressions.Add(newInstance); return(Expression.Block(notTrackedExpressions)); }
/// <summary> /// Generates an expression tree to clone an ISerializable /// </summary> /// <param name="variables">Global variables for the expression tree</param> /// <param name="source">Source object</param> /// <param name="clone">Clone object</param> /// <param name="sourceType">Type of the source object</param> /// <param name="refTrackerParam">Reference tracker</param> /// <returns>An expression tree to clone an ISerializable</returns> public static Expression GenerateISerializableExpression(List <ParameterExpression> variables, ParameterExpression source, ParameterExpression clone, Type sourceType, ParameterExpression refTrackerParam) { var serializationConstructor = ISerializableSerializer.GetSerializationConstructor(sourceType); if (serializationConstructor == null) { throw new MissingConstructorException("Cannot serialize type " + sourceType + " because it does not have the required constructor for ISerializable. If you inherits from a class that implements ISerializable you have to expose the serialization constructor."); } var getEnumeratorMethodInfo = SerializationInfoMih.GetEnumerator(); var siSource = Expression.Parameter(typeof(SerializationInfo), "siSource"); var fc = Expression.Parameter(typeof(FormatterConverter), "fc"); var context = Expression.Parameter(typeof(StreamingContext), "context"); var iserSource = Expression.Parameter(typeof(ISerializable), "iserSource"); var siClone = Expression.Parameter(typeof(SerializationInfo), "siClone"); var cloner = Expression.Parameter(typeof(Func <object, ObjectClonerReferenceTracker, object>), "cloner"); var clonedItem = Expression.Parameter(typeof(object), "clonedItem"); variables.Add(fc); variables.Add(context); variables.Add(siSource); variables.Add(iserSource); variables.Add(siClone); variables.Add(cloner); variables.Add(clonedItem); var enumeratorMethod = Expression.Call(siSource, getEnumeratorMethodInfo); var loopBodyCargo = new EnumerableLoopBodyCargo(); loopBodyCargo.EnumeratorType = typeof(SerializationInfoEnumerator); loopBodyCargo.KvpType = typeof(SerializationEntry); var expressions = new List <Expression>(); expressions.Add(Expression.Assign(fc, Expression.New(typeof(FormatterConverter)))); expressions.Add(Expression.Assign(context, Expression.New(StreamingContextMih.Constructor(), Expression.Constant(StreamingContextStates.All)))); expressions.Add(Expression.Assign(siSource, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(sourceType), fc))); expressions.Add(Expression.Assign(iserSource, Expression.Convert(source, typeof(ISerializable)))); expressions.Add(Expression.Assign(siClone, Expression.New(SerializationInfoMih.Constructor(), Expression.Constant(sourceType), fc))); expressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializingAttributeExpression(sourceType, source, context)); expressions.Add(Expression.Call(iserSource, ISerializableMih.GetObjectData(), siSource, context)); expressions.AddRange(SerializationCallbacksHelper.GenerateOnSerializedAttributeExpression(sourceType, source, context)); expressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(siSource, "IsFullTypeNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the full type name for an ISerializable is not supported"))))); expressions.Add(Expression.IfThen(Expression.IsTrue(Expression.Property(siSource, "IsAssemblyNameSetExplicit")), Expression.Throw(Expression.New(InvalidOperationExceptionMih.Constructor(), Expression.Constant("Changing the assembly name for an ISerializable is not supported"))))); expressions.Add(EnumerableLoopHelper.GenerateEnumeratorLoop(variables, GetLoopBodyCargo(siClone, clonedItem, refTrackerParam), enumeratorMethod, null, loopBodyCargo)); expressions.Add(Expression.Assign(clone, Expression.New(serializationConstructor, siClone, context))); expressions.Add(Expression.Call(refTrackerParam, ObjectClonerReferenceTrackerMih.Track(), source, clone)); expressions.AddRange(SerializationCallbacksHelper.GenerateOnDeserializedAttributeExpression(sourceType, clone, context)); expressions.Add(SerializationCallbacksHelper.GenerateCallIDeserializationExpression(sourceType, clone)); return(ObjectCloner.GenerateNullTrackedOrUntrackedExpression(source, clone, sourceType, refTrackerParam, Expression.Block(expressions))); }