Esempio n. 1
0
        static void GenerateAssembly(Type[] types)
        {
            Dictionary <Type, TypeData> map = GenerateTypeData(types);

            var nonStaticTypes = map.Where(kvp => kvp.Value.IsDynamic).Select(kvp => kvp.Key);

            var ab   = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("NetSerializerDebug"), AssemblyBuilderAccess.RunAndSave);
            var modb = ab.DefineDynamicModule("NetSerializerDebug.dll");
            var tb   = modb.DefineType("NetSerializer", TypeAttributes.Public);

            /* generate stubs */
            foreach (var type in nonStaticTypes)
            {
                var mb = SerializerCodegen.GenerateStaticSerializerStub(tb, type);
                map[type].WriterMethodInfo = mb;
                map[type].WriterILGen      = mb.GetILGenerator();
            }

            foreach (var type in nonStaticTypes)
            {
                var dm = DeserializerCodegen.GenerateStaticDeserializerStub(tb, type);
                map[type].ReaderMethodInfo = dm;
                map[type].ReaderILGen      = dm.GetILGenerator();
            }

            var serializerSwitchMethod = tb.DefineMethod("SerializerSwitch", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(Stream), typeof(object) });

            serializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            serializerSwitchMethod.DefineParameter(2, ParameterAttributes.None, "value");
            var serializerSwitchMethodInfo = serializerSwitchMethod;

            var deserializerSwitchMethod = tb.DefineMethod("DeserializerSwitch", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(Stream), typeof(object).MakeByRefType() });

            deserializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            deserializerSwitchMethod.DefineParameter(2, ParameterAttributes.Out, "value");
            var deserializerSwitchMethodInfo = deserializerSwitchMethod;

            var ctx = new CodeGenContext(map, serializerSwitchMethodInfo, deserializerSwitchMethodInfo);

            /* generate bodies */
            foreach (var type in nonStaticTypes)
            {
                SerializerCodegen.GenerateSerializerBody(ctx, type, map[type].WriterILGen);
            }

            foreach (var type in nonStaticTypes)
            {
                DeserializerCodegen.GenerateDeserializerBody(ctx, type, map[type].ReaderILGen);
            }

            var ilGen = serializerSwitchMethod.GetILGenerator();

            SerializerCodegen.GenerateSerializerSwitch(ctx, ilGen, map);

            ilGen = deserializerSwitchMethod.GetILGenerator();
            DeserializerCodegen.GenerateDeserializerSwitch(ctx, ilGen, map);

            tb.CreateType();
            ab.Save("NetSerializerDebug.dll");
        }
Esempio n. 2
0
        static void GenerateDebugAssembly(Dictionary <Type, TypeData> map)
        {
            var ab   = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("NetSerializerDebug"), AssemblyBuilderAccess.RunAndSave);
            var modb = ab.DefineDynamicModule("NetSerializerDebug.dll");
            var tb   = modb.DefineType("NetSerializer", TypeAttributes.Public);

            /* generate stubs */
            foreach (var kvp in map)
            {
                var type = kvp.Key;
                var td   = kvp.Value;

                if (!td.IsGenerated)
                {
                    continue;
                }

                var mb = SerializerCodegen.GenerateStaticSerializerStub(tb, type);
                td.WriterMethodInfo = mb;
                td.WriterILGen      = mb.GetILGenerator();

                var dm = DeserializerCodegen.GenerateStaticDeserializerStub(tb, type);
                td.ReaderMethodInfo = dm;
                td.ReaderILGen      = dm.GetILGenerator();
            }

            var serializerSwitchMethod = tb.DefineMethod("SerializerSwitch", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(Stream), typeof(object) });

            serializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            serializerSwitchMethod.DefineParameter(2, ParameterAttributes.None, "value");
            var serializerSwitchMethodInfo = serializerSwitchMethod;

            var deserializerSwitchMethod = tb.DefineMethod("DeserializerSwitch", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(Stream), typeof(object).MakeByRefType() });

            deserializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            deserializerSwitchMethod.DefineParameter(2, ParameterAttributes.Out, "value");
            var deserializerSwitchMethodInfo = deserializerSwitchMethod;

            var ctx = new CodeGenContext(map, serializerSwitchMethodInfo, deserializerSwitchMethodInfo);

            /* generate bodies */

            foreach (var kvp in map)
            {
                var type = kvp.Key;
                var td   = kvp.Value;

                if (!td.IsGenerated)
                {
                    continue;
                }

                td.TypeSerializer.GenerateWriterMethod(type, ctx, td.WriterILGen);
                td.TypeSerializer.GenerateReaderMethod(type, ctx, td.ReaderILGen);
            }

            var ilGen = serializerSwitchMethod.GetILGenerator();

            SerializerCodegen.GenerateSerializerSwitch(ctx, ilGen, map);

            ilGen = deserializerSwitchMethod.GetILGenerator();
            DeserializerCodegen.GenerateDeserializerSwitch(ctx, ilGen, map);

            tb.CreateType();
            ab.Save("NetSerializerDebug.dll");
        }
Esempio n. 3
0
        static Dictionary <Type, ushort> GenerateDynamic(Type[] types)
        {
            Dictionary <Type, TypeData> map = GenerateTypeData(types);

            var nonStaticTypes = map.Where(kvp => kvp.Value.IsDynamic).Select(kvp => kvp.Key);

            /* generate stubs */
            foreach (var type in nonStaticTypes)
            {
                var dm = SerializerCodegen.GenerateDynamicSerializerStub(type);
                map[type].WriterMethodInfo = dm;
                map[type].WriterILGen      = dm.GetILGenerator();
            }

            foreach (var type in nonStaticTypes)
            {
                var dm = DeserializerCodegen.GenerateDynamicDeserializerStub(type);
                map[type].ReaderMethodInfo = dm;
                map[type].ReaderILGen      = dm.GetILGenerator();
            }

            var serializerSwitchMethod = new DynamicMethod("SerializerSwitch", null,
                                                           new Type[] { typeof(Stream), typeof(object) },
                                                           typeof(Serializer), true);

            serializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            serializerSwitchMethod.DefineParameter(2, ParameterAttributes.None, "value");
            var serializerSwitchMethodInfo = serializerSwitchMethod;

            var deserializerSwitchMethod = new DynamicMethod("DeserializerSwitch", null,
                                                             new Type[] { typeof(Stream), typeof(object).MakeByRefType() },
                                                             typeof(Serializer), true);

            deserializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            deserializerSwitchMethod.DefineParameter(2, ParameterAttributes.Out, "value");
            var deserializerSwitchMethodInfo = deserializerSwitchMethod;

            var ctx = new CodeGenContext(map, serializerSwitchMethodInfo, deserializerSwitchMethodInfo);

            /* generate bodies */
            foreach (var type in nonStaticTypes)
            {
                SerializerCodegen.GenerateSerializerBody(ctx, type, map[type].WriterILGen);
            }

            foreach (var type in nonStaticTypes)
            {
                DeserializerCodegen.GenerateDeserializerBody(ctx, type, map[type].ReaderILGen);
            }

            var ilGen = serializerSwitchMethod.GetILGenerator();

            SerializerCodegen.GenerateSerializerSwitch(ctx, ilGen, map);
            s_serializerSwitch = (SerializerSwitch)serializerSwitchMethod.CreateDelegate(typeof(SerializerSwitch));

            ilGen = deserializerSwitchMethod.GetILGenerator();
            DeserializerCodegen.GenerateDeserializerSwitch(ctx, ilGen, map);
            s_deserializerSwitch = (DeserializerSwitch)deserializerSwitchMethod.CreateDelegate(typeof(DeserializerSwitch));

            return(map.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.TypeID));
        }
Esempio n. 4
0
        static void GenerateDynamic(Dictionary <Type, TypeData> map)
        {
            /* generate stubs */
            foreach (var kvp in map)
            {
                var type = kvp.Key;
                var td   = kvp.Value;

                if (!td.IsGenerated)
                {
                    continue;
                }

                var writerDm = SerializerCodegen.GenerateDynamicSerializerStub(type);
                td.WriterMethodInfo = writerDm;
                td.WriterILGen      = writerDm.GetILGenerator();

                var readerDm = DeserializerCodegen.GenerateDynamicDeserializerStub(type);
                td.ReaderMethodInfo = readerDm;
                td.ReaderILGen      = readerDm.GetILGenerator();
            }

            var serializerSwitchMethod = new DynamicMethod("SerializerSwitch", null,
                                                           new Type[] { typeof(Stream), typeof(object) },
                                                           typeof(Serializer), true);

            serializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            serializerSwitchMethod.DefineParameter(2, ParameterAttributes.None, "value");
            var serializerSwitchMethodInfo = serializerSwitchMethod;

            var deserializerSwitchMethod = new DynamicMethod("DeserializerSwitch", null,
                                                             new Type[] { typeof(Stream), typeof(object).MakeByRefType() },
                                                             typeof(Serializer), true);

            deserializerSwitchMethod.DefineParameter(1, ParameterAttributes.None, "stream");
            deserializerSwitchMethod.DefineParameter(2, ParameterAttributes.Out, "value");
            var deserializerSwitchMethodInfo = deserializerSwitchMethod;

            var ctx = new CodeGenContext(map, serializerSwitchMethodInfo, deserializerSwitchMethodInfo);

            /* generate bodies */

            foreach (var kvp in map)
            {
                var type = kvp.Key;
                var td   = kvp.Value;

                if (!td.IsGenerated)
                {
                    continue;
                }

                td.TypeSerializer.GenerateWriterMethod(type, ctx, td.WriterILGen);
                td.TypeSerializer.GenerateReaderMethod(type, ctx, td.ReaderILGen);
            }

            var ilGen = serializerSwitchMethod.GetILGenerator();

            SerializerCodegen.GenerateSerializerSwitch(ctx, ilGen, map);
            s_serializerSwitch = (SerializerSwitch)serializerSwitchMethod.CreateDelegate(typeof(SerializerSwitch));

            ilGen = deserializerSwitchMethod.GetILGenerator();
            DeserializerCodegen.GenerateDeserializerSwitch(ctx, ilGen, map);
            s_deserializerSwitch = (DeserializerSwitch)deserializerSwitchMethod.CreateDelegate(typeof(DeserializerSwitch));
        }