Esempio n. 1
0
        public static void CompileAssembly(List <Type> userTypes, IBaseSerializer[] userSerializers, ISerializationPredicates userPredicates, string assemblyName, string assemblyDir, string typeName)
        {
            var serializer = new FastSerializer();

            serializer.Initialize(userTypes, userSerializers, userPredicates);
            EmitHelper.EmitAssembly(assemblyName, assemblyDir, typeName, serializer._emitCtx);
        }
Esempio n. 2
0
        public static FastSerializer BindSerializer(Type serializerType)
        {
            var serializer = new FastSerializer();

            if (serializerType == null)
            {
                throw new ArgumentNullException("serializerType");
            }

            var serializeMethod = serializerType.GetMethod("Serialize", BindingFlags.Static | BindingFlags.Public);

            if (serializeMethod == null)
            {
                throw new NullReferenceException("Serializer type: " + serializerType + " must have a public static method called `Serialize` that could be pointed to via a SerializationDelegate delegate");
            }

            var deserializeMethod = serializerType.GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public);

            if (deserializeMethod == null)
            {
                throw new NullReferenceException("Serializer type: " + serializerType + " must have a public static method called `Deserialize` that could be pointed to via a DeserializationDelegate delegate");
            }

            serializer._serialize   = (SerializationDelegate)Delegate.CreateDelegate(typeof(SerializationDelegate), null, serializeMethod);
            serializer._deserialize = (DeserializationDelegate)Delegate.CreateDelegate(typeof(DeserializationDelegate), null, deserializeMethod);

            return(serializer);
        }
Esempio n. 3
0
        public static void CompileDynamicDbg(List <Type> types, string asmname, string typename)
        {
            var serializer = new FastSerializer();

            serializer.Initialize(types, null, null);
            EmitHelper.EmitDbgAsm(serializer, types, asmname, null, typename, serializer._emitCtx);
        }
Esempio n. 4
0
 public SerializationContext(FastSerializer serializer)
 {
     this.Serializer = serializer;
 }
Esempio n. 5
0
        public static FastSerializer CompileDynamic(List <Type> userTypes, IBaseSerializer[] userSerializers, ISerializationPredicates userPredicates, Action <Type> onTypeGenerated)
        {
            var serializer = new FastSerializer();

            serializer._onTypeGenerated = onTypeGenerated;
            serializer.Initialize(userTypes, userSerializers, userPredicates);
            var ctx = serializer._emitCtx;

            EmitHelper.EmitDynamic(ctx, out serializer._serialize, out serializer._deserialize);

            // generate write null and serialize delegators
            {
                //public static void Write_Null(Stream, value, ctx)
                //{
                //}

                // foreach type generate a delegator to the actual write method
                //public static void Delegate_Write_X(Stream, object value, ctx)
                //{
                //    Write_X(stream, (X)value, ctx);
                //}

                var emit = EmitHelper.emit;

                var writeNull = new DynamicMethod("Write_Null", typeof(void), EmitHelper.SerializationParameters);
                {
                    emit.il = writeNull.GetILGenerator();
                    emit.ret();
                }

                var writeNullDelegate = writeNull.CreateDelegate <SerializationDelegate>();
                serializer._serializationDelegates.Add(writeNullDelegate);

                for (int i = 0; i < ctx.Types.Count; i++)
                {
                    var type      = ctx.Types[i];
                    var writer    = ctx.GetWriteStub(type);
                    var delegator = EmitHelper.EmitDynamicSerializeDelegator(type, writer);
                    serializer._serializationDelegates.Add(delegator);
                }
            }

            // generate read null and deserialize delegators
            {
                //public static object Read_Null(Stream, ctx)
                //{
                //  return null;
                //}

                // foreach type generate a delegator to the actual read method
                //public static object Delegate_Read_X(Stream, ctx)
                //{
                //    return <box if needed>Read_X(stream, ctx);
                //}

                var emit = EmitHelper.emit;

                var readNull = new DynamicMethod("Read_Null", typeof(object), EmitHelper.DeserializationParameters);
                {
                    emit.il = readNull.GetILGenerator();
                    emit.retnull();
                }

                var readNullDelegate = readNull.CreateDelegate <DeserializationDelegate>();
                serializer._deserializationDelegates.Add(readNullDelegate);

                for (int i = 0; i < ctx.Types.Count; i++)
                {
                    var type      = ctx.Types[i];
                    var reader    = ctx.GetReadStub(type);
                    var delegator = EmitHelper.EmitDynamicDeserializeDelegator(type, reader);
                    serializer._deserializationDelegates.Add(delegator);
                }
            }

            return(serializer);
        }