static FormatterCache() { Type t = typeof(T); BssomFormatterAttribute attr = t.GetCustomAttribute <BssomFormatterAttribute>(); if (attr == null) { return; } Type formatterType = attr.FormatterType; if (formatterType.IsGenericType && !formatterType.IsConstructedGenericType) { formatterType = formatterType.MakeGenericType(t.GetGenericArguments()); } if (formatterType != t) { throw BssomSerializationTypeFormatterException.AttributeFormatterTypeMismatch(formatterType, t); } if (attr.Arguments == null) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(formatterType); } else { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(formatterType, attr.Arguments); } }
//Size IEnumerable public static int SizeIEnumerable(ref BssomSizeContext context, IEnumerable value) { if (value == null) { return(BssomBinaryPrimitives.NullSize); } IBssomFormatter <object> formatter = context.Option.FormatterResolver.GetFormatterWithVerify <object>(); long dataLen = 0; foreach (object item in value) { dataLen += formatter.Size(ref context, item); } if (value is ICollection coll) { return(BssomBinaryPrimitives.Array2TypeSize(coll.Count, dataLen)); } else { return(BssomBinaryPrimitives.Array2TypeSizeWithFixU32Count(dataLen)); } }
static FormatterCache() { if (typeof(T) == typeof(object)) { Formatter = (IBssomFormatter <T>)(IBssomFormatter) ObjectFormatter.Instance; } }
static FormatterCache() { if (typeof(T) == typeof(string)) { Formatter = (IBssomFormatter <T>)(object) MyStringFormatter.Instance; } }
static FormatterCache() { Type t = typeof(T); if (Array1ResolverGetFormatterHelper.TryGetFormatter(t, out IBssomFormatter formatter)) { Formatter = (IBssomFormatter <T>)formatter; return; } if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ArraySegment <>)) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(ArraySegmentFormatter <>).MakeGenericType(t.GetGenericArguments())); return; } if (TypeIsArray(t, out int rank, out Type elementType)) { if (rank == 1) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(OneDimensionalArrayFormatter <>).MakeGenericType(elementType)); } else if (rank == 2) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(TwoDimensionalArrayFormatter <>).MakeGenericType(elementType)); } else if (rank == 3) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(ThreeDimensionalArrayFormatter <>).MakeGenericType(elementType)); } else if (rank == 4) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(FourDimensionalArrayFormatter <>).MakeGenericType(elementType)); } else { throw BssomSerializationTypeFormatterException.UnsupportedType(t); } return; } if (TypeIsCollection(t, out ConstructorInfo constructor, out Type itemType, out bool isImplGenerIList, out bool IsImplIList, out bool isImplGenerICollec, out bool isImplIReadOnlyList)) { TypeInfo buildType; if (t.IsInterface) { buildType = ICollectionFormatterTypeBuilder.BuildICollectionInterfaceType(DynamicAssembly, t, itemType); } else { buildType = ICollectionFormatterTypeBuilder.BuildICollectionImplementationType(DynamicAssembly, t, constructor, itemType, isImplGenerIList, IsImplIList, isImplGenerICollec, isImplIReadOnlyList); } Formatter = (IBssomFormatter <T>)Activator.CreateInstance(buildType); } }
internal static bool TryGetFormatter(Type t, out IBssomFormatter formatter) { if (FormatterMap.TryGetValue(t, out formatter)) { return(true); } return(false); }
static FormatterCache() { Type t = typeof(T); if (PrimitiveResolverGetFormatterHelper.TryGetFormatter(t, out IBssomFormatter formatter)) { Formatter = (IBssomFormatter <T>)formatter; } }
static FormatterCache() { Type t = typeof(T); if (BssomValueResolverGetFormatterHelper.TryGetFormatter(t, out IBssomFormatter formatter)) { Formatter = (IBssomFormatter <T>)formatter; return; } }
static FormatterCache() { foreach (IFormatterResolver item in Resolvers) { IBssomFormatter <T> f = item.GetFormatter <T>(); if (f != null) { Formatter = f; return; } } }
static FormatterCache() { Type t = typeof(T); if (BuildInResolverGetFormatterHelper.TryGetFormatter(t, out IBssomFormatter formatter)) { Formatter = (IBssomFormatter <T>)formatter; return; } if (t.IsEnum) { Formatter = EnumFormatter <T> .Instance; return; } if (t.IsGenericType) { Type genericType = t.GetGenericTypeDefinition(); if (genericType == typeof(Nullable <>)) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(NullableFormatter <>).MakeGenericType(t.GetGenericArguments())); return; } else if (genericType == typeof(Lazy <>)) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(LazyFormatter <>).MakeGenericType(t.GetGenericArguments())); return; } else if (genericType == typeof(IGrouping <,>)) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(InterfaceGroupingFormatter <,>).MakeGenericType(t.GetGenericArguments())); return; } else if (genericType == typeof(ILookup <,>)) { Formatter = (IBssomFormatter <T>)Activator.CreateInstance(typeof(InterfaceILookupFormatter <,>).MakeGenericType(t.GetGenericArguments())); return; } } if (t.IsAnonymousType()) { Formatter = AnonymousTypeFormatter <T> .Instance; return; } }
//Serialize IEnumerable<> public static void SerializeGenericIEnumerable <TElement>(ref BssomWriter writer, ref BssomSerializeContext context, IEnumerable <TElement> value) { if (value == null) { writer.WriteNull(); return; } IBssomFormatter <TElement> formatter = context.Option.FormatterResolver.GetFormatterWithVerify <TElement>(); writer.WriteArray2Type(); long pos = writer.FillUInt32FixNumber(); if (value.TryGetICollectionCount(out int count)) { writer.WriteVariableNumber(count); foreach (TElement item in value) { context.CancellationToken.ThrowIfCancellationRequested(); formatter.Serialize(ref writer, ref context, item); } writer.WriteBackFixNumber(pos, checked ((int)(writer.Position - pos - BssomBinaryPrimitives.FixUInt32NumberSize))); } else { count = 0; long posCount = writer.FillUInt32FixNumber(); foreach (TElement item in value) { context.CancellationToken.ThrowIfCancellationRequested(); count++; formatter.Serialize(ref writer, ref context, item); } long cPos = writer.Position; writer.BufferWriter.Seek(pos); writer.WriteBackFixNumber(checked ((int)(writer.Position - posCount))); writer.WriteBackFixNumber(count); writer.BufferWriter.Seek(cPos); } }
//Serialize IList public static void SerializeIList(ref BssomWriter writer, ref BssomSerializeContext context, IList value) { if (value == null) { writer.WriteNull(); return; } IBssomFormatter <object> formatter = context.Option.FormatterResolver.GetFormatterWithVerify <object>(); writer.WriteArray2Type(); long pos = writer.FillUInt32FixNumber(); writer.WriteVariableNumber(value.Count); for (int i = 0; i < value.Count; i++) { context.CancellationToken.ThrowIfCancellationRequested(); formatter.Serialize(ref writer, ref context, value[i]); } writer.WriteBackFixNumber(pos, checked ((int)(writer.Position - pos - BssomBinaryPrimitives.FixUInt32NumberSize))); }
static FormatterCache() { Type t = typeof(T); if (t == typeof(IDictionary)) { Formatter = (IBssomFormatter <T>)((IBssomFormatter)IDictionaryFormatter.Instance); return; } if (TypeIsDictionary(t, out ConstructorInfo constructor, out bool typeIsGeneric, out Type genericTypeDefinition, out Type genericKeyType, out Type genericValueType)) { if (typeIsGeneric && genericKeyType != typeof(object) && !BssMapKeyResolverProvider.TryGetBssMapKeyResolver(genericKeyType, out IBssMapKeyResolver keyConvert)) { return;// throw BssomSerializationException.MapKeyTypeError(); } TypeInfo buildType; if (typeIsGeneric) { if (t.IsInterface) { buildType = IDictionaryFormatterTypeBuilder.BuildGenericIDictionaryInterfaceType(DynamicAssembly, t, genericTypeDefinition, genericKeyType, genericValueType); } else { buildType = IDictionaryFormatterTypeBuilder.BuildGenericIDictionaryImplementationType(DynamicAssembly, constructor, t, genericKeyType, genericValueType); } } else { //impl IDictionary class/struct buildType = IDictionaryFormatterTypeBuilder.BuildIDictionaryImplementationType(DynamicAssembly, constructor, t); } Formatter = (IBssomFormatter <T>)Activator.CreateInstance(buildType); } }
//Size IEnumerable<> public static int SizeGenericIEnumerable <TElement>(ref BssomSizeContext context, IEnumerable <TElement> value) { if (value == null) { return(BssomBinaryPrimitives.NullSize); } IBssomFormatter <TElement> formatter = context.Option.FormatterResolver.GetFormatterWithVerify <TElement>(); long dataLen = 0; foreach (TElement item in value) { dataLen += formatter.Size(ref context, item); } if (value.TryGetICollectionCount(out int count)) { return(BssomBinaryPrimitives.Array2TypeSize(count, dataLen)); } else { return(BssomBinaryPrimitives.Array2TypeSizeWithFixU32Count(dataLen)); } }
//Serialize IList<> / IReadOnlyList<> public static void SerializeGenerIList <TElement>(ref BssomWriter writer, ref BssomSerializeContext context, IEnumerable <TElement> value) { if (value == null) { writer.WriteNull(); } else { DEBUG.Assert(value is IList <TElement> || value is IReadOnlyList <TElement>); IBssomFormatter <TElement> formatter = context.Option.FormatterResolver.GetFormatterWithVerify <TElement>(); writer.WriteArray2Type(); long pos = writer.FillUInt32FixNumber(); if (value is IList <TElement> list) { writer.WriteVariableNumber(list.Count); for (int i = 0; i < list.Count; i++) { context.CancellationToken.ThrowIfCancellationRequested(); formatter.Serialize(ref writer, ref context, list[i]); } } else { IReadOnlyList <TElement> rels = Unsafe.As <IEnumerable <TElement>, IReadOnlyList <TElement> >(ref value); writer.WriteVariableNumber(rels.Count); for (int i = 0; i < rels.Count; i++) { context.CancellationToken.ThrowIfCancellationRequested(); formatter.Serialize(ref writer, ref context, rels[i]); } } writer.WriteBackFixNumber(pos, checked ((int)(writer.Position - pos - BssomBinaryPrimitives.FixUInt32NumberSize))); } }
static FormatterCache() { Type t = typeof(T); Formatter = (IBssomFormatter <T>)Activator.CreateInstance(Array3CodeGenResolverBuilder.Build(DynamicAssembly, new ObjectSerializationInfo(t, false))); }