public override void Execute(Type objType, Type interfaceType, TypeBuilder builder, IEnumerable <AccelFieldInfo> fields, int fieldCount, bool hasContinuousSerialIndexes, MethodInfo beforeMethod, MethodInfo afterMethod)
        {
            MethodBuilder methodBuilder = RuntimeInjector.DefineSerializeMethod(builder, objType, interfaceType);
            ILGenerator   il            = methodBuilder.GetILGenerator();

            EmitIL(il, objType, fields, beforeMethod);
            il.Emit(OpCodes.Ret);
        }
Exemplo n.º 2
0
        private static Type InjectType(Type objType, AccelTypeInfo typeInfo)
        {
            TypeBuilder builder = RuntimeInjector.DefineSerializerType(objType, out Type interfaceType);

            AccelTypeInfo.GetTypeInfo(typeInfo, out var fields, out var before, out var after, out var fieldCount, out var hasContinuousSerialIndexes);

            s_Progress0.Execute(objType, interfaceType, builder, fields, fieldCount, hasContinuousSerialIndexes, before, after);
            s_Progress1.Execute(objType, interfaceType, builder, fields, fieldCount, hasContinuousSerialIndexes, before, after);
            s_Progress2.Execute(objType, interfaceType, builder, fields, fieldCount, hasContinuousSerialIndexes, before, after);

            return(builder.CreateType());
        }
Exemplo n.º 3
0
        private void DefineType(string packageName, AccelTypeInfo declaringType, TypeBuilder declaringTypeBuilder, StructDeclaration def, Dictionary <string, AccelTypeInfo> result)
        {
            string name     = def.Name;
            string fullName = def.IsNested ? $"{declaringType.FullName}+{def.Name}" : $"{packageName}.{def.Name}";
            Dictionary <string, AccelFieldInfo> fields = new Dictionary <string, AccelFieldInfo>(def.Declarations.Declarations.Length);
            Type          baseType = def.IsRef ? typeof(object) : typeof(ValueType);
            TypeBuilder   builder  = RuntimeInjector.DefinePublicType(def.IsNested ? name : fullName, def.IsFinal, declaringTypeBuilder, baseType, Type.EmptyTypes);
            AccelTypeInfo info     = new AccelTypeInfo(name, fullName, def.Doc, TypeVisibility.Public, def.IsFinal, def.IsRef, def.IsNested, def.IsFieldIndexContinuous, (int)def.Size, declaringType, fields);

            for (int i = 0; i < def.Declarations.Declarations.Length; i++)
            {
                IDeclaration declaration = def.Declarations.Declarations[i];

                switch (declaration)
                {
                case FieldDeclaration fieldDeclaration:
                    Type fieldType = GetType(fieldDeclaration.Type);
                    Type realType  = fieldDeclaration.RealType == null ? null : GetType(fieldDeclaration.RealType);
                    builder.DefineField(fieldDeclaration.Name, fieldType, FieldAttributes.Public);
                    AccelFieldInfo fieldInfo = new AccelFieldInfo(fieldDeclaration.Name, fieldDeclaration.Doc, (int)fieldDeclaration.Index, fieldDeclaration.IsObsolete, fieldDeclaration.IsNeverNull, info, fieldType, realType);
                    fields.Add(fieldDeclaration.Name, fieldInfo);
                    break;

                case StructDeclaration structDeclaration:
                    DefineType(packageName, info, builder, structDeclaration, result);
                    break;

                default:
                    continue;
                }
            }

            Type type = builder.CreateType();

            info.TypeHandle = type.TypeHandle;

            foreach (FieldInfo field in type.GetFields())
            {
                fields[field.Name].FieldHandle = field.FieldHandle;
            }

            result.Add(fullName, info);
        }
Exemplo n.º 4
0
 internal static Expression Call(this MethodInfo method, object instance, RuntimeInjector injector)
 {
     return(Expression.Call(Expression.Constant(instance, method.DeclaringType), method, injector.Inject(method)));
 }