Example #1
0
        private void DefineDynamicDeserializeFunction(AccelTypeInfo typeInfo)
        {
            DynamicMethod method = new DynamicMethod(Resources.DeserializeMethodName + typeInfo.FullName, typeof(object), s_DynamicDeserializeFunctionArgs);
            ILGenerator   il     = method.GetILGenerator();

            EmitDynamicDeserializeFunctionIL(il, typeInfo.Info);
            DynamicDeserializeFunction func = method.CreateDelegate(typeof(DynamicDeserializeFunction)) as DynamicDeserializeFunction;

            typeInfo.DeserializeFunction = func;
        }
        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());
        }
Example #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);
        }
 public static Type Inject(Type objectType, AccelTypeInfo typeInfo)
 {
     return(InjectType(objectType, typeInfo));
 }
Example #5
0
 /// <summary>
 /// 序列化对象,并将序列化数据写入缓冲区中
 /// </summary>
 /// <param name="obj">被序列化的对象</param>
 /// <param name="type">序列化的对象类型</param>
 /// <param name="encoding">序列化使用的字符编码</param>
 /// <param name="endian">序列化使用的字节序</param>
 /// <returns>对象的序列化结果</returns>
 public static byte[] Serialize(object obj, AccelTypeInfo type, Encoding encoding = Encoding.UTF8, Endian endian = Endian.BigEndian)
 {
     return(type.SerializeFunction(obj, encoding, endian));
 }