public IFormatter GetFormatter(Type type) { if (typeof(MemberInfo).IsAssignableFrom(type)) { var memberInfoFormatterType = typeof(MemberInfoFormatter <>).MakeGenericType(type); var memberInfoFormatter = Activator.CreateInstance(memberInfoFormatterType, args: _ceras); return((IFormatter)memberInfoFormatter); } if (typeof(MulticastDelegate).IsAssignableFrom(type)) { if (_ceras.Config.Advanced.DelegateSerialization == DelegateSerializationFlags.Off) { throw new InvalidOperationException($"The type '{type.FullName}' can not be serialized because it is a delegate; and 'config.Advanced.DelegateSerialization' is turned off."); } // Every delegate type is created by the formatter, there can't be any exceptions (unless you do some really dangerous stuff) CerasSerializer.AddFormatterConstructedType(type); var formatterType = typeof(DelegateFormatter <>).MakeGenericType(type); var formatter = Activator.CreateInstance(formatterType, args: _ceras); return((IFormatter)formatter); } return(null); }
public CollectionFormatter(CerasSerializer serializer) { var itemType = typeof(TItem); _itemFormatter = (IFormatter <TItem>)serializer.GetReferenceFormatter(itemType); _maxSize = serializer.Config.Advanced.SizeLimits.MaxCollectionSize; var collectionType = typeof(TCollection); if (collectionType.IsGenericType) { ConstructorInfo ctor = null; if (collectionType.GetGenericTypeDefinition() == typeof(List <>)) { // Special case: List<> ctor = collectionType.GetConstructor(new Type[] { typeof(int) }); } else if (collectionType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { // Special case: Dictionary<,> ctor = collectionType.GetConstructor(new Type[] { typeof(int) }); } if (ctor != null && serializer.Config.Advanced.AotMode == AotMode.None) { var sizeArg = Expression.Parameter(typeof(int)); _capacityConstructor = Expression.Lambda <Func <int, TCollection> >(Expression.New(ctor, sizeArg), sizeArg).Compile(); CerasSerializer.AddFormatterConstructedType(collectionType); } } }
public IFormatter GetFormatter(Type type) { if (_primitiveFormatters.TryGetValue(type, out var f)) { return(f); } if (type.IsGenericType) { var genericDef = type.GetGenericTypeDefinition(); if (genericDef == typeof(Nullable <>)) { // Find the closed type, it's possible the current type is not generic itself, but derives from a closed generic var closedType = ReflectionHelper.FindClosedType(type, typeof(Nullable <>)); var formatterType = typeof(NullableFormatter <>).MakeGenericType(closedType.GetGenericArguments()); return((IFormatter)Activator.CreateInstance(formatterType, _ceras)); } if (genericDef == typeof(KeyValuePair <,>)) { // Find the closed type, it's possible the current type is not generic itself, but derives from a closed generic var closedType = ReflectionHelper.FindClosedType(type, typeof(KeyValuePair <,>)); var formatterType = typeof(KeyValuePairFormatter <,>).MakeGenericType(closedType.GetGenericArguments()); return((IFormatter)Activator.CreateInstance(formatterType)); } if (_iTupleInterface.IsAssignableFrom(type)) { if (type.IsValueType) // ValueTuple { var nArgs = type.GenericTypeArguments.Length; var formatterType = _valueTupleFormatterTypes[nArgs]; formatterType = formatterType.MakeGenericType(type.GenericTypeArguments); var formatter = (IFormatter)Activator.CreateInstance(formatterType); CerasSerializer.AddFormatterConstructedType(type); return(formatter); } if (type.IsClass) // Tuple { var nArgs = type.GenericTypeArguments.Length; var formatterType = _tupleFormatterTypes[nArgs]; formatterType = formatterType.MakeGenericType(type.GenericTypeArguments); var formatter = (IFormatter)Activator.CreateInstance(formatterType); CerasSerializer.AddFormatterConstructedType(type); return(formatter); } } } return(null); }
public DictionaryFormatter(CerasSerializer serializer) { var itemType = typeof(KeyValuePair <TKey, TValue>); _itemFormatter = (IFormatter <KeyValuePair <TKey, TValue> >)serializer.GetReferenceFormatter(itemType); // We'll handle instantiation ourselves in order to call the capacity ctor CerasSerializer.AddFormatterConstructedType(typeof(Dictionary <TKey, TValue>)); }
public ListFormatter(CerasSerializer serializer) { var itemType = typeof(TItem); _itemFormatter = (IFormatter <TItem>)serializer.GetReferenceFormatter(itemType); // We'll handle instantiation ourselves in order to call the capacity ctor CerasSerializer.AddFormatterConstructedType(typeof(List <TItem>)); }
public IFormatter GetFormatter(Type type) { if (_primitiveFormatters.TryGetValue(type, out var f)) { return(f); } if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof(Nullable <>)) { var innerType = type.GetGenericArguments()[0]; var formatterType = typeof(NullableFormatter <>).MakeGenericType(innerType); return((IFormatter)Activator.CreateInstance(formatterType, _serializer)); } if (_iTupleInterface.IsAssignableFrom(type)) { #if NETSTANDARD if (type.IsValueType) // ValueTuple { var nArgs = type.GenericTypeArguments.Length; var formatterType = _valueTupleFormatterTypes[nArgs]; formatterType = formatterType.MakeGenericType(type.GenericTypeArguments); var formatter = (IFormatter)Activator.CreateInstance(formatterType); _serializer.InjectDependencies(formatter); CerasSerializer.AddFormatterConstructedType(type); return(formatter); } #endif if (type.IsClass) // Tuple { var nArgs = type.GenericTypeArguments.Length; var formatterType = _tupleFormatterTypes[nArgs]; formatterType = formatterType.MakeGenericType(type.GenericTypeArguments); var formatter = (IFormatter)Activator.CreateInstance(formatterType); _serializer.InjectDependencies(formatter); CerasSerializer.AddFormatterConstructedType(type); return(formatter); } } } return(null); }
public IFormatter GetFormatter(Type type) { if (type.IsSubclassOf(typeof(Expression))) { // No type derived from Expression can be instantiated normally CerasSerializer.AddFormatterConstructedType(type); return(_expressionFormatter); } if (type == typeof(LabelTarget)) { return(_labelTargetFormatter); } return(null); }
protected CollectionByProxyFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(TCollection)); }
public MemberMemberBindingFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(MemberMemberBinding)); }
public ImmutableQueueFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(ImmutableQueue <TItem>)); }
public LabelFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(LabelExpression)); }
public LabelTargetFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(LabelTarget)); }
public MemberAssignmentFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(MemberAssignment)); }
public StackFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(Stack <TItem>)); }
static UlidFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(Ulid)); }
public BitArrayFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(BitArray)); }
public UriFormatter() { CerasSerializer.AddFormatterConstructedType(typeof(Uri)); }