Example #1
0
        public static Type Compile(Type payloadType, ref JsonSerializerOptions options)
        {
            if (options == null)
            {
                options = DefaultSerializerOptions;
            }

            TypeBuilder tb = JitAssembly.JitModuleBuilder.DefineType(
                name: JitAssembly.GeneratedModuleNamespace + @".ObjectSerialier" + Interlocked.Increment(ref _compiledCount),
                attr: TypeAttributes.Public | TypeAttributes.Sealed,
                parent: typeof(ValueType),
                interfaces: new Type[] { typeof(ISerialierImplementation <>).MakeGenericType(payloadType) }
                );

            JitILCompiler compiler = new JitILCompiler(payloadType, options, tb);

            // Compile the non chunk routine first since the chunk routine needs to know the number of labels.
            compiler.GenerateNonChunkMethod();
            compiler.GenerateChunkMethod();
            compiler.GenerateResetMethod();

            Type compiledSerializerType = tb.CreateType();

            compiler._followUp(compiledSerializerType);

            return(compiledSerializerType);
        }