public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { _ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null); try { BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "FromJson", typeof(JsonFormatClassReaderDelegate), memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag) { classContract.RequiresMemberAccessForRead(securityException); } else { throw; } } InitArgs(); CreateObject(classContract); _ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal); InvokeOnDeserializing(classContract); if (classContract.IsISerializable) ReadISerializable(classContract); else ReadClass(classContract); if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { _ilg.Call(_objectLocal, JsonFormatGeneratorStatics.OnDeserializationMethod, null); } InvokeOnDeserialized(classContract); if (!InvokeFactoryMethod(classContract)) { _ilg.Load(_objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); _ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType); } //Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>. else if (classContract.IsKeyValuePairAdapter) { _ilg.Call(classContract.GetKeyValuePairMethodInfo); _ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType); } else { _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType); } } return (JsonFormatClassReaderDelegate)_ilg.EndMethod(); }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { this.ilg = new CodeGenerator(); bool allowPrivateMemberAccess = classContract.RequiresMemberAccessForRead(null); try { this.ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, allowPrivateMemberAccess); } catch (SecurityException exception) { if (!allowPrivateMemberAccess || !exception.PermissionType.Equals(typeof(ReflectionPermission))) { throw; } classContract.RequiresMemberAccessForRead(exception); } this.InitArgs(); this.DemandSerializationFormatterPermission(classContract); this.DemandMemberAccessPermission(allowPrivateMemberAccess); this.CreateObject(classContract); this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, this.objectLocal); this.InvokeOnDeserializing(classContract); LocalBuilder local = null; if (this.HasFactoryMethod(classContract)) { local = this.ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead"); this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod); this.ilg.Stloc(local); } if (classContract.IsISerializable) { this.ReadISerializable(classContract); } else { this.ReadClass(classContract); } if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { this.ilg.Call(this.objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); } this.InvokeOnDeserialized(classContract); if ((local == null) || !this.InvokeFactoryMethod(classContract, local)) { this.ilg.Load(this.objectLocal); if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { this.ilg.ConvertValue(this.objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); this.ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); this.ilg.ConvertValue(Globals.TypeOfDateTimeOffset, this.ilg.CurrentMethod.ReturnType); } else { this.ilg.ConvertValue(this.objectLocal.LocalType, this.ilg.CurrentMethod.ReturnType); } } return((XmlFormatClassReaderDelegate)this.ilg.EndMethod()); }
public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { _ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null, JsonGlobals.JsonSerializationPatterns); try { BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "FromJson", typeof(JsonFormatClassReaderDelegate), memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag) { classContract.RequiresMemberAccessForRead(securityException, JsonGlobals.JsonSerializationPatterns); } else { throw; } } InitArgs(); CreateObject(classContract); _ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal); InvokeOnDeserializing(classContract); if (classContract.IsISerializable) { ReadISerializable(classContract); } else { ReadClass(classContract); } InvokeOnDeserialized(classContract); if (!InvokeFactoryMethod(classContract)) { _ilg.Load(_objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); _ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType); } //Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>. else if (classContract.IsKeyValuePairAdapter) { _ilg.Call(classContract.GetKeyValuePairMethodInfo); _ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType); } else { _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType); } } return((JsonFormatClassReaderDelegate)_ilg.EndMethod()); }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null); ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag); InitArgs(); CreateObject(classContract); ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, objectLocal); InvokeOnDeserializing(classContract); LocalBuilder objectId = null; if (HasFactoryMethod(classContract)) { objectId = ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead"); ilg.Call(contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod); ilg.Stloc(objectId); } if (classContract.IsISerializable) { ReadISerializable(classContract); } else { ReadClass(classContract); } bool isFactoryType = InvokeFactoryMethod(classContract, objectId); if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { ilg.Call(objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); } InvokeOnDeserialized(classContract); if (objectId == null || !isFactoryType) { ilg.Load(objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); ilg.ConvertValue(Globals.TypeOfDateTimeOffset, ilg.CurrentMethod.ReturnType); } else { ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType); } } return((XmlFormatClassReaderDelegate)ilg.EndMethod()); }
static internal void InvokeOnDeserialized(object value, DataContract contract, XmlObjectSerializerReadContextComplexJson context) { if (contract is ClassDataContract) { ClassDataContract classContract = contract as ClassDataContract; if (classContract.BaseContract != null) { InvokeOnDeserialized(value, classContract.BaseContract, context); } if (classContract.OnDeserialized != null) { bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null, JsonGlobals.JsonSerializationPatterns); try { DisallowMemberAccess(memberAccessFlag); classContract.OnDeserialized.Invoke(value, new object[] { context.GetStreamingContext() }); } catch (SecurityException securityException) { if (memberAccessFlag) { classContract.RequiresMemberAccessForRead(securityException, JsonGlobals.JsonSerializationPatterns); } else { throw; } } catch (TargetInvocationException targetInvocationException) { if (targetInvocationException.InnerException == null) { throw; } //We are catching the TIE here and throws the inner exception only, //this is needed to have a consistent exception story in all serializers throw targetInvocationException.InnerException; } } } }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { if (DataContractSerializer.Option == SerializationOption.ReflectionOnly) { return(new ReflectionXmlClassReader(classContract).ReflectionReadClass); } #if uapaot else if (DataContractSerializer.Option == SerializationOption.ReflectionAsBackup) { return(new ReflectionXmlClassReader(classContract).ReflectionReadClass); } #endif else { #if uapaot throw new InvalidOperationException("Cannot generate class reader"); #else _ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null); try { _ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag) { classContract.RequiresMemberAccessForRead(securityException); } else { throw; } } InitArgs(); CreateObject(classContract); _ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal); InvokeOnDeserializing(classContract); LocalBuilder objectId = null; if (HasFactoryMethod(classContract)) { objectId = _ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead"); _ilg.Call(_contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod); _ilg.Stloc(objectId); } if (classContract.IsISerializable) { ReadISerializable(classContract); } else { ReadClass(classContract); } bool isFactoryType = InvokeFactoryMethod(classContract, objectId); if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { _ilg.Call(_objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); } InvokeOnDeserialized(classContract); if (objectId == null) { _ilg.Load(_objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); _ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType); } //Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>. else if (classContract.IsKeyValuePairAdapter) { _ilg.Call(classContract.GetKeyValuePairMethodInfo); _ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType); } else { _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType); } } return((XmlFormatClassReaderDelegate)_ilg.EndMethod()); #endif } }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null); try { ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag && securityException.PermissionType.Equals(typeof(ReflectionPermission))) { classContract.RequiresMemberAccessForRead(securityException); } else { throw; } } InitArgs(); DemandSerializationFormatterPermission(classContract); DemandMemberAccessPermission(memberAccessFlag); CreateObject(classContract); ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, objectLocal); InvokeOnDeserializing(classContract); LocalBuilder objectId = null; if (HasFactoryMethod(classContract)) { objectId = ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead"); ilg.Call(contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod); ilg.Stloc(objectId); } if (classContract.IsISerializable) ReadISerializable(classContract); else ReadClass(classContract); bool isFactoryType = InvokeFactoryMethod(classContract, objectId); if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) ilg.Call(objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); InvokeOnDeserialized(classContract); if (objectId == null || !isFactoryType) { ilg.Load(objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); ilg.ConvertValue(Globals.TypeOfDateTimeOffset, ilg.CurrentMethod.ReturnType); } else { ilg.ConvertValue(objectLocal.LocalType, ilg.CurrentMethod.ReturnType); } } return (XmlFormatClassReaderDelegate)ilg.EndMethod(); }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { this.ilg = new CodeGenerator(); bool allowPrivateMemberAccess = classContract.RequiresMemberAccessForRead(null); try { this.ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, allowPrivateMemberAccess); } catch (SecurityException exception) { if (!allowPrivateMemberAccess || !exception.PermissionType.Equals(typeof(ReflectionPermission))) { throw; } classContract.RequiresMemberAccessForRead(exception); } this.InitArgs(); this.DemandSerializationFormatterPermission(classContract); this.DemandMemberAccessPermission(allowPrivateMemberAccess); this.CreateObject(classContract); this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, this.objectLocal); this.InvokeOnDeserializing(classContract); LocalBuilder local = null; if (this.HasFactoryMethod(classContract)) { local = this.ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead"); this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod); this.ilg.Stloc(local); } if (classContract.IsISerializable) { this.ReadISerializable(classContract); } else { this.ReadClass(classContract); } if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { this.ilg.Call(this.objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); } this.InvokeOnDeserialized(classContract); if ((local == null) || !this.InvokeFactoryMethod(classContract, local)) { this.ilg.Load(this.objectLocal); if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { this.ilg.ConvertValue(this.objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); this.ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); this.ilg.ConvertValue(Globals.TypeOfDateTimeOffset, this.ilg.CurrentMethod.ReturnType); } else { this.ilg.ConvertValue(this.objectLocal.LocalType, this.ilg.CurrentMethod.ReturnType); } } return (XmlFormatClassReaderDelegate) this.ilg.EndMethod(); }
public XmlFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract) { if (DataContractSerializer.Option == SerializationOption.ReflectionOnly) { return new ReflectionXmlClassReader(classContract).ReflectionReadClass; } #if NET_NATIVE else if (DataContractSerializer.Option == SerializationOption.ReflectionAsBackup) { return new ReflectionXmlClassReader(classContract).ReflectionReadClass; } #endif else { #if NET_NATIVE throw new InvalidOperationException("Cannot generate class reader"); #else _ilg = new CodeGenerator(); bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null); try { _ilg.BeginMethod("Read" + classContract.StableName.Name + "FromXml", Globals.TypeOfXmlFormatClassReaderDelegate, memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag) { classContract.RequiresMemberAccessForRead(securityException); } else { throw; } } InitArgs(); CreateObject(classContract); _ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal); InvokeOnDeserializing(classContract); LocalBuilder objectId = null; if (classContract.IsISerializable) { ReadISerializable(classContract); } else { ReadClass(classContract); } if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType)) { _ilg.Call(_objectLocal, XmlFormatGeneratorStatics.OnDeserializationMethod, null); } InvokeOnDeserialized(classContract); if (objectId == null) { _ilg.Load(_objectLocal); // Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization. // DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation // on DateTimeOffset; which does not work in partial trust. if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter) { _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter); _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod); _ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType); } //Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>. else if (classContract.IsKeyValuePairAdapter) { _ilg.Call(classContract.GetKeyValuePairMethodInfo); _ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType); } else { _ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType); } } return (XmlFormatClassReaderDelegate)_ilg.EndMethod(); #endif } }