//apply to IDictionary impl public static TypeInfo BuildIDictionaryImplementationType(DynamicFormatterAssembly assembly, ConstructorInfo constructor, Type type) { TypeBuilder typeBuilder = assembly.DefineFormatterType(type); MethodBuilder serializeMethod = TypeBuildHelper.DefineSerializeMethod(typeBuilder, type); TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(MapFormatterHelper).GetMethod(nameof(MapFormatterHelper.SerializeIDictionary), BindingFlags.Public | BindingFlags.Static)); MethodBuilder deserializeMethod = TypeBuildHelper.DefineDeserializeMethod(typeBuilder, type); ParameterInfo[] args = constructor.GetParameters(); Type dynamicCacheType = typeof(IDictionaryDynamicDelegateCache <>).MakeGenericType(type); if (args.Length == 1) { DEBUG.Assert(args[0].ParameterType == typeof(IDictionary)); //return new T(IDictionaryFormatter.Deserialize) MethodInfo methodinfo = dynamicCacheType.GetMethod(nameof(IDictionaryDynamicDelegateCache <int> .GenerateInjectCtor)); methodinfo.Invoke(null, new object[] { constructor, args[0].ParameterType }); } else { MethodInfo methodinfo = dynamicCacheType.GetMethod(nameof(IDictionaryDynamicDelegateCache <int> .GenerateDeserializeWithIDictionaryEmptyCtor)); methodinfo.Invoke(null, new object[] { }); } TypeBuildHelper.CallDeserializeDelegate(deserializeMethod, type, dynamicCacheType.GetField(nameof(IDictionaryDynamicDelegateCache <int> .Deserialize), BindingFlags.Public | BindingFlags.Static)); MethodBuilder sizeMethod = TypeBuildHelper.DefineSizeMethod(typeBuilder, type); TypeBuildHelper.CallOneMethodInSize(sizeMethod, typeof(MapFormatterHelper).GetMethod(nameof(MapFormatterHelper.SizeIDictionary), BindingFlags.Public | BindingFlags.Static)); return(typeBuilder.CreateTypeInfo()); }
public static TypeInfo Build(DynamicFormatterAssembly assembly, ObjectSerializationInfo serializationInfo) { Type type = serializationInfo.Type; TypeBuilder typeBuilder = assembly.DefineFormatterType(type); serializationInfo.SerializeMemberInfosOrderByKeyIndex(type); MethodBuilder serializeMethod = TypeBuildHelper.DefineSerializeMethod(typeBuilder, type); MethodBuilder deserializeMethod = TypeBuildHelper.DefineDeserializeMethod(typeBuilder, type); MethodBuilder sizeMethod = TypeBuildHelper.DefineSizeMethod(typeBuilder, type); Type delegateCacheType = typeof(Array3DelegateCache <>).MakeGenericType(type); delegateCacheType.GetMethod(nameof(Array3DelegateCache <int> .Factory), BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { assembly, serializationInfo }); TypeBuildHelper.CallSerializeDelegate(serializeMethod, type, delegateCacheType.GetField(nameof(Array3DelegateCache <int> .Serialize))); TypeBuildHelper.CallSizeDelegate(sizeMethod, type, delegateCacheType.GetField(nameof(Array3DelegateCache <int> .Size))); TypeBuildHelper.CallDeserializeDelegate(deserializeMethod, type, delegateCacheType.GetField(nameof(Array3DelegateCache <int> .Deserialize))); return(typeBuilder.CreateTypeInfo()); }
public static TypeInfo BuildICollectionImplementationType(DynamicFormatterAssembly assembly, Type type, ConstructorInfo constructor, Type itemType, bool isImplGenerIList, bool IsImplIList, bool isImplGenerICollec, bool isImplIReadOnlyList) { TypeBuilder typeBuilder = assembly.DefineFormatterType(type); MethodBuilder sizeMethod = TypeBuildHelper.DefineSizeMethod(typeBuilder, type); MethodBuilder serializeMethod = TypeBuildHelper.DefineSerializeMethod(typeBuilder, type); if (itemType == typeof(object)) { //itemType is Object, Array2 if (IsImplIList) { TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SerializeIList), BindingFlags.Public | BindingFlags.Static)); } else { TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SerializeIEnumerable), BindingFlags.Public | BindingFlags.Static)); } TypeBuildHelper.CallOneMethodInSize(sizeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SizeIEnumerable), BindingFlags.Public | BindingFlags.Static)); } else { if (Array1FormatterHelper.IsArray1Type(itemType)) { //Array1 TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(Array1FormatterHelper).GetMethod(nameof(Array1FormatterHelper.SerializeIEnumerable), new Type[] { typeof(BssomWriter).MakeByRefType(), typeof(BssomSerializeContext).MakeByRefType(), typeof(IEnumerable <>).MakeGenericType(itemType) })); TypeBuildHelper.CallOneMethodInSize(sizeMethod, typeof(Array1FormatterHelper).GetMethod(nameof(Array1FormatterHelper.SizeIEnumerable), new Type[] { typeof(BssomSizeContext).MakeByRefType(), typeof(IEnumerable <>).MakeGenericType(itemType) })); } else { if (isImplGenerIList || isImplIReadOnlyList) { TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SerializeGenerIList), BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(itemType)); } else { TypeBuildHelper.CallOneMethodInSerialize(serializeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SerializeGenericIEnumerable), BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(itemType)); } TypeBuildHelper.CallOneMethodInSize(sizeMethod, typeof(Array2FormatterHelper).GetMethod(nameof(Array2FormatterHelper.SizeGenericIEnumerable), BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(itemType)); } } MethodBuilder deserializeMethod = TypeBuildHelper.DefineDeserializeMethod(typeBuilder, type); ParameterInfo[] args = constructor.GetParameters(); if (args.Length == 1 && args[0].ParameterType != typeof(int)) { //new T(IEnumerable t) Type dynamicCacheType = typeof(CollectionDynamicDelegateCache <>).MakeGenericType(type); MethodInfo methodinfo = dynamicCacheType.GetMethod(nameof(CollectionDynamicDelegateCache <int> .GenerateInjectCtor)); methodinfo.Invoke(null, new object[] { constructor, args[0].ParameterType }); TypeBuildHelper.CallDeserializeDelegate(deserializeMethod, type, dynamicCacheType.GetField(nameof(CollectionDynamicDelegateCache <int> .Deserialize), BindingFlags.Public | BindingFlags.Static)); } else { if (itemType == typeof(DateTime))//DateTime需要特殊处理,因为要处理Standrand和Native { Type dtCollBuilder = typeof(DateTimeCollectionDeserializeBuilder <>).MakeGenericType(type); MethodInfo methodinfo = dtCollBuilder.GetMethod(nameof(DateTimeCollectionDeserializeBuilder <ICollection <DateTime> > .ConstructorInit)); methodinfo.Invoke(null, new object[] { constructor }); TypeBuildHelper.CallOneMethodInDeserialize(deserializeMethod, dtCollBuilder.GetMethod(nameof(DateTimeCollectionDeserializeBuilder <ICollection <DateTime> > .Deserialize))); } else { Type dynamicCacheType = typeof(CollectionDynamicDelegateCache <>).MakeGenericType(type); if (args.Length == 0) { MethodInfo methodinfo = dynamicCacheType.GetMethod(nameof(CollectionDynamicDelegateCache <int> .GenerateDeserializeWithEmptyCtor)); methodinfo.Invoke(null, new object[] { constructor, isImplGenerICollec, itemType }); } else { DEBUG.Assert(args.Length == 1 && args[0].ParameterType == typeof(int)); MethodInfo methodinfo = dynamicCacheType.GetMethod(nameof(CollectionDynamicDelegateCache <int> .GenerateDeserializeWithCapacityCtor)); methodinfo.Invoke(null, new object[] { constructor, isImplGenerICollec, itemType }); } TypeBuildHelper.CallDeserializeDelegate(deserializeMethod, type, dynamicCacheType.GetField(nameof(CollectionDynamicDelegateCache <int> .Deserialize), BindingFlags.Public | BindingFlags.Static)); } } return(typeBuilder.CreateTypeInfo()); }