private void GenerateISerializableDeserializationConstructor() { // deserialisation constructor ParameterSpec[] constrParams = new ParameterSpec[] { new ParameterSpec("info", typeof(System.Runtime.Serialization.SerializationInfo)), new ParameterSpec("context", typeof(System.Runtime.Serialization.StreamingContext)) }; ConstructorBuilder constrBuilder = m_ilEmitHelper.AddConstructor(m_builder, constrParams, MethodAttributes.Family | MethodAttributes.HideBySig); ILGenerator body = constrBuilder.GetILGenerator(); // value type constructors don't call base class constructors // directly start with deserialisation MethodInfo getValueMethod = typeof(System.Runtime.Serialization.SerializationInfo).GetMethod("GetValue", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string), typeof(Type) }, new ParameterModifier[0]); MethodInfo getTypeFromH = ReflectionHelper.TypeType.GetMethod("GetTypeFromHandle", BindingFlags.Public | BindingFlags.Static); Label beforeReturn = body.DefineLabel(); // get the value of the init field GenerateGetFieldFromObjectData(m_initalizedField, body, getValueMethod, getTypeFromH); // nothing more to do, if not initalized, therefore check here body.Emit(OpCodes.Ldarg_0); body.Emit(OpCodes.Ldfld, m_initalizedField); // fieldvalue body.Emit(OpCodes.Brfalse, beforeReturn); // initalized -> deserialise discriminator and corresponding value GenerateGetFieldFromObjectData(m_discrField, body, getValueMethod, getTypeFromH); // now deserialize field corresponding to discriminator value GenerateAssignFromInfoForDiscAction action = new GenerateAssignFromInfoForDiscAction(this, getValueMethod, getTypeFromH); GenerateSwitchForDiscriminator(body, action, beforeReturn); body.MarkLabel(beforeReturn); body.Emit(OpCodes.Ret); }
private void GenerateISerializableDeserializationConstructor() { // deserialisation constructor ParameterSpec[] constrParams = new ParameterSpec[] { new ParameterSpec("info", typeof(System.Runtime.Serialization.SerializationInfo)), new ParameterSpec("context", typeof(System.Runtime.Serialization.StreamingContext)) }; ConstructorBuilder constrBuilder = m_ilEmitHelper.AddConstructor(m_builder, constrParams, MethodAttributes.Family | MethodAttributes.HideBySig); ILGenerator body = constrBuilder.GetILGenerator(); // value type constructors don't call base class constructors // directly start with deserialisation MethodInfo getValueMethod = typeof(System.Runtime.Serialization.SerializationInfo).GetMethod("GetValue", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(string), typeof(Type) }, new ParameterModifier[0]); MethodInfo getTypeFromH = ReflectionHelper.TypeType.GetMethod("GetTypeFromHandle", BindingFlags.Public | BindingFlags.Static); Label beforeReturn = body.DefineLabel(); // get the value of the init field GenerateGetFieldFromObjectData(m_initalizedField, body, getValueMethod, getTypeFromH); // nothing more to do, if not initalized, therefore check here body.Emit(OpCodes.Ldarg_0); body.Emit(OpCodes.Ldfld, m_initalizedField); // fieldvalue body.Emit(OpCodes.Brfalse, beforeReturn); // initalized -> deserialise discriminator and corresponding value GenerateGetFieldFromObjectData(m_discrField, body, getValueMethod, getTypeFromH); // now deserialize field corresponding to discriminator value GenerateAssignFromInfoForDiscAction action = new GenerateAssignFromInfoForDiscAction(this, getValueMethod, getTypeFromH); GenerateSwitchForDiscriminator(body, action, beforeReturn); body.MarkLabel(beforeReturn); body.Emit(OpCodes.Ret); }