public static void GenerateLoadEx(this ITypeDescriptor descriptor, IILGen ilGenerator, Action <IILGen> pushReader, Action <IILGen> pushCtx, Action <IILGen> pushDescriptor, Type asType, ITypeConvertorGenerator convertorGenerator) { if (descriptor.StoredInline) { if (descriptor.LoadNeedsHelpWithConversion && asType != typeof(object)) { var origType = descriptor.GetPreferedType(); descriptor.GenerateLoad(ilGenerator, pushReader, pushCtx, pushDescriptor, origType); if (origType != asType) { var conv = convertorGenerator.GenerateConversion(origType, asType); if (conv == null) { throw new BTDBException("Don't know how to convert " + descriptor.Name + " from " + origType.ToSimpleName() + " to " + asType.ToSimpleName()); } conv(ilGenerator); } } else { descriptor.GenerateLoad(ilGenerator, pushReader, pushCtx, pushDescriptor, asType); } } else { ilGenerator .Do(pushCtx) .Callvirt(() => default(ITypeBinaryDeserializerContext).LoadObject()); if (asType != typeof(object)) { ilGenerator.Castclass(asType); } } }
Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeSerializersId2LoaderMapping, ITypeDescriptor, object> LoaderFactory(ITypeDescriptor descriptor) { var loadAsType = LoadAsType(descriptor); var methodBuilder = ILBuilder.Instance.NewMethod <Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeSerializersId2LoaderMapping, ITypeDescriptor, object> >("DeserializerFor" + descriptor.Name); var il = methodBuilder.Generator; if (descriptor.AnyOpNeedsCtx()) { var localCtx = il.DeclareLocal(typeof(ITypeBinaryDeserializerContext), "ctx"); var haveCtx = il.DefineLabel(); il .Ldarg(1) .Dup() .Stloc(localCtx) .Brtrue(haveCtx) .Ldarg(0) .Ldarg(2) .Newobj(() => new DeserializerCtx(null, null)) .Castclass(typeof(ITypeBinaryDeserializerContext)) .Stloc(localCtx) .Mark(haveCtx); descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldloc(localCtx), ilGen => ilGen.Ldarg(3), loadAsType); } else { descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldarg(1), ilGen => ilGen.Ldarg(3), loadAsType); } if (loadAsType.IsValueType) { il.Box(loadAsType); } else if (loadAsType != typeof(object)) { il.Castclass(typeof(object)); } il.Ret(); return(methodBuilder.Create()); }
Func<AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeSerializersId2LoaderMapping, ITypeDescriptor, object> LoaderFactory(ITypeDescriptor descriptor) { var loadAsType = LoadAsType(descriptor); var methodBuilder = ILBuilder.Instance.NewMethod<Func<AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeSerializersId2LoaderMapping, ITypeDescriptor, object>>("DeserializerFor" + descriptor.Name); var il = methodBuilder.Generator; if (descriptor.AnyOpNeedsCtx()) { var localCtx = il.DeclareLocal(typeof(ITypeBinaryDeserializerContext), "ctx"); var haveCtx = il.DefineLabel(); il .Ldarg(1) .Dup() .Stloc(localCtx) .Brtrue(haveCtx) .Ldarg(0) .Ldarg(2) .Newobj(() => new DeserializerCtx(null, null)) .Castclass(typeof(ITypeBinaryDeserializerContext)) .Stloc(localCtx) .Mark(haveCtx); descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldloc(localCtx), ilGen => ilGen.Ldarg(3), loadAsType); } else { descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldarg(1), ilGen => ilGen.Ldarg(3), loadAsType); } if (loadAsType.IsValueType) { il.Box(loadAsType); } else if (loadAsType != typeof(object)) { il.Castclass(typeof(object)); } il.Ret(); return methodBuilder.Create(); }
Func<AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object> LoaderFactory(ITypeDescriptor descriptor) { var loadAsType = LoadAsType(descriptor); var methodBuilder = ILBuilder.Instance.NewMethod<Func<AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object>>("DeserializerFor" + descriptor.Name); var il = methodBuilder.Generator; descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldarg(1), ilGen => ilGen.Ldarg(2), loadAsType); if (loadAsType.IsValueType) { il.Box(loadAsType); } else if (loadAsType != typeof(object)) { il.Castclass(typeof(object)); } il.Ret(); return methodBuilder.Create(); }