Esempio n. 1
0
        public IntPtr BuildReader(Type type, bool ignoreCustomSerialization)
        {
            var constantsBuilder = module.DefineType(type.Name + "_GroBufReader_" + Guid.NewGuid(), TypeAttributes.Class | TypeAttributes.Public);

            constantsBuilder.DefineField("pointers", typeof(IntPtr[]), FieldAttributes.Private | FieldAttributes.Static);
            constantsBuilder.DefineField("delegates", typeof(Delegate[]), FieldAttributes.Private | FieldAttributes.Static);
            var constantsBuilderContext = new ReaderConstantsBuilderContext(groBufReader, constantsBuilder, readerCollection, dataMembersExtractor);

            constantsBuilderContext.BuildConstants(type, true, ignoreCustomSerialization);
            var constantsType = constantsBuilder.CreateTypeInfo();
            var fields        = constantsBuilderContext.GetFields().ToDictionary(pair => pair.Key, pair => pair.Value.Select(constantsType.GetField).ToArray());
            var context       = new ReaderTypeBuilderContext(groBufReader, module, constantsType, fields, readerCollection, dataMembersExtractor);
            var reader        = context.GetReader(type, true, ignoreCustomSerialization);

            var initializer = BuildInitializer(constantsType.GetField("pointers", BindingFlags.Static | BindingFlags.NonPublic), constantsType.GetField("delegates", BindingFlags.Static | BindingFlags.NonPublic));

            var compiledDynamicMethods = context.GetMethods();
            var pointers  = new IntPtr[compiledDynamicMethods.Length];
            var delegates = new Delegate[compiledDynamicMethods.Length];

            foreach (var pair in compiledDynamicMethods)
            {
                var compiledDynamicMethod = pair.Value;
                var index = compiledDynamicMethod.Index;
                pointers[index]  = compiledDynamicMethod.Pointer;
                delegates[index] = compiledDynamicMethod.Delegate;
                if (compiledDynamicMethod.Pointer != reader.Pointer)
                {
                    groBufReader.readMethodsWithCustomSerialization[pair.Key] = (IntPtr?)compiledDynamicMethod.Pointer;
                }
            }
            initializer(pointers, delegates, context.GetFieldInitializers());
            return(reader.Pointer);
        }
Esempio n. 2
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     foreach (var argumentType in Type.GetGenericArguments())
     {
         context.BuildConstants(argumentType);
     }
 }
Esempio n. 3
0
        protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
        {
            context.SetFields(Type, new[]
            {
                new KeyValuePair <string, Type>("setters_" + Type.Name + "_" + Guid.NewGuid(), typeof(IntPtr[])),
                new KeyValuePair <string, Type>("delegates_" + Type.Name + "_" + Guid.NewGuid(), typeof(Delegate[])),
                new KeyValuePair <string, Type>("hashCodes_" + Type.Name + "_" + Guid.NewGuid(), typeof(ulong[])),
            });
            foreach (var member in context.GetDataMembers(Type))
            {
                Type memberType;
                switch (member.Member.MemberType)
                {
                case MemberTypes.Property:
                    memberType = ((PropertyInfo)member.Member).PropertyType;
                    break;

                case MemberTypes.Field:
                    memberType = ((FieldInfo)member.Member).FieldType;
                    break;

                default:
                    throw new NotSupportedException("Data member of type " + member.Member.MemberType + " is not supported");
                }
                context.BuildConstants(memberType);
            }
        }
Esempio n. 4
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.SetFields(Type, new[]
     {
         new KeyValuePair <string, Type>("readers_" + Type.Name + "_" + Guid.NewGuid(), typeof(IntPtr[])),
         new KeyValuePair <string, Type>("delegates_" + Type.Name + "_" + Guid.NewGuid(), typeof(Delegate[])),
     });
 }
Esempio n. 5
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.SetFields(Type, new[]
     {
         new KeyValuePair <string, Type>("readers_" + Type.Name + "_" + Guid.NewGuid(), typeof(IntPtr[])),
         new KeyValuePair <string, Type>("delegates_" + Type.Name + "_" + Guid.NewGuid(), typeof(Delegate[]))    // This field is needed only to save references to the dynamic methods. Otherwise GC will destroy them
     });
     Array.ForEach(GroBufHelpers.LeafTypes.Where(type => type != null).ToArray(), type => context.BuildConstants(type));
 }
Esempio n. 6
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.SetFields(Type, new[]
     {
         new KeyValuePair <string, Type>("values_" + Type.Name + "_" + Guid.NewGuid(), typeof(int[])),
         new KeyValuePair <string, Type>("hashCodes_" + Type.Name + "_" + Guid.NewGuid(), typeof(ulong[])),
     });
     context.BuildConstants(typeof(int));
     context.BuildConstants(typeof(string));
 }
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.BuildConstants(elementType);
 }
Esempio n. 8
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.SetFields(Type, new[] { new KeyValuePair <string, Type>("customSerializer_" + Type.Name + "_" + Guid.NewGuid(), typeof(IGroBufCustomSerializer)) });
 }
Esempio n. 9
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.BuildConstants(typeof(byte[]));
 }
Esempio n. 10
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
 }
Esempio n. 11
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.BuildConstants(Type.GetGenericArguments()[0]);
 }
Esempio n. 12
0
 protected abstract void BuildConstantsInternal(ReaderConstantsBuilderContext context);
Esempio n. 13
0
 public void BuildConstants(ReaderConstantsBuilderContext context)
 {
     context.SetFields(Type, new KeyValuePair <string, Type> [0]);
     BuildConstantsInternal(context);
 }
Esempio n. 14
0
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.BuildConstants(keyType);
     context.BuildConstants(valueType);
 }
 protected override void BuildConstantsInternal(ReaderConstantsBuilderContext context)
 {
     context.BuildConstants(typeof(DateTime));
     context.BuildConstants(typeof(short));
 }