Пример #1
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (!type.IsGenericType)
            {
                handler = null;
                return(false);
            }

            var genericTypeDefinition = type.GetGenericTypeDefinition();

            if (genericTypeDefinition == typeof(Pair <>))
            {
                var actualType  = type.GetGenericArguments().Single();
                var constructor = typeof(PairTrwHandler <>).MakeGenericType(actualType).GetConstructor(Type.EmptyTypes);
                Debug.Assert(constructor != null, "constructor != null");
                handler = (ITrwSerializationHandler)constructor.Invoke(EmptyArrays <object> .Array);
                return(true);
            }

            // todo: Pair<T1, T2>
            // todo: UnorderedPair

            handler = null;
            return(false);
        }
 public TrwSerializationReadContext(ITrwReader reader, ITrwSerializationHandlerContainer handlers, IReadOnlyDictionary <string, Type> typeAliases, TrwSerializationOptions options)
 {
     Reader           = reader;
     this.handlers    = handlers;
     this.typeAliases = typeAliases;
     this.options     = options;
     Bag = new Dictionary <string, object>();
     reader.MoveNext();
 }
Пример #3
0
 public TrwSerializationWriteContext(ITrwWriter stream, ITrwSerializationHandlerContainer handlers,
                                     IReadOnlyList <ITrwSerializationTypeRedirect> typeRedirects, TrwSerializationOptions options)
 {
     Writer             = stream;
     TypeAliases        = new Dictionary <Type, string>();
     this.handlers      = handlers;
     this.typeRedirects = typeRedirects;
     this.options       = options;
     Bag = new Dictionary <string, object>();
 }
Пример #4
0
 public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
 {
     if (typeof(IAsset).IsAssignableFrom(type))
     {
         handler = new AssetTrwHandler();
         return(true);
     }
     handler = null;
     return(false);
 }
 public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
 {
     if (typeof(IResource).IsAssignableFrom(type))
     {
         var handlerCtor = typeof(ResourceTrwHandler <>).MakeGenericType(type).GetConstructor(Type.EmptyTypes);
         Debug.Assert(handlerCtor != null, nameof(handlerCtor) + " != null");
         handler = (ITrwSerializationHandler)handlerCtor.Invoke(EmptyArrays <object> .Array);
         return(true);
     }
     handler = null;
     return(false);
 }
Пример #6
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (type.GetCustomAttribute <TrwSerializeAttribute>() != null)
            {
                var constructor = typeof(AttributeTrwHandler <>).MakeGenericType(type).GetConstructor(new [] { typeof(ITrwAttributeObjectCreator) });
                Debug.Assert(constructor != null, "constructor != null");
                handler = (ITrwSerializationHandler)constructor.Invoke(new object[] { objectCreator });
                return(true);
            }

            handler = null;
            return(false);
        }
Пример #7
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (!type.IsArray)
            {
                handler = null;
                return(false);
            }

            var constructor = typeof(ArrayTrwHandler <>).MakeGenericType(type.GetElementType()).GetConstructor(Type.EmptyTypes);

            Debug.Assert(constructor != null, nameof(constructor) + " != null");
            handler = (ITrwSerializationHandler)constructor.Invoke(new object[0]);
            return(true);
        }
Пример #8
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (type == typeof(Vector4))
            {
                handler = new Vector4TrwHandler();
                return(true);
            }
            if (type == typeof(Vector3))
            {
                handler = new Vector3TrwHandler();
                return(true);
            }
            if (type == typeof(Vector2))
            {
                handler = new Vector2TrwHandler();
                return(true);
            }
            if (type == typeof(Transform))
            {
                handler = new TransformTrwHandler();
                return(true);
            }
            if (type == typeof(Color4))
            {
                handler = new ProxyTrwHandler <Color4, Vector4>(x => x.Raw, x => new Color4(x), false);
                return(true);
            }
            if (type == typeof(Quaternion))
            {
                handler = new ProxyTrwHandler <Quaternion, Vector4>(x => x.Raw, x => new Quaternion(x), false);
                return(true);
            }
            if (type == typeof(AaRectangle2))
            {
                handler = new ProxyTrwHandler <AaRectangle2, Vector4>(
                    x => new Vector4(x.Center.X, x.Center.Y, x.HalfWidth, x.HalfHeight),
                    x => new AaRectangle2(new Vector2(x.X, x.Y), x.Z, x.W),
                    false);
                return(true);
            }
            if (type == typeof(IntSize2))
            {
                handler = new ProxyTrwHandler <IntSize2, Vector2>(x => new Vector2(x.Width, x.Height), x => new IntSize2((int)x.X, (int)x.Y), false);
                return(true);
            }

            handler = null;
            return(false);
        }
Пример #9
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (typeof(IAmObject).IsAssignableFrom(type))
            {
                var handlerCtor =
                    typeof(AmObjectTrwHandler <>).MakeGenericType(type)
                    .GetConstructor(new[] { typeof(IAmDiBasedObjectFactory) });
                Debug.Assert(handlerCtor != null, "handlerCtor != null");
                handler = (ITrwSerializationHandler)handlerCtor.Invoke(new object[] { objectFactory });
                return(true);
            }

            handler = null;
            return(false);
        }
 public TrwSerializationDiffApplier(ITrwSerializationHandlerContainer handlers, IReadOnlyList <ITrwSerializationTypeRedirect> typeRedirects,
                                    Action <IDictionary <string, object> > fillWriteBag, Action <IDictionary <string, object> > fillReadBag)
 {
     this.handlers        = handlers;
     this.typeRedirects   = typeRedirects;
     this.fillWriteBag    = fillWriteBag;
     this.fillReadBag     = fillReadBag;
     serializationOptions = new TrwSerializationOptions
     {
         ExplicitTypes     = TrwSerializationExplicitTypes.WhenObject,
         ValuePropertyName = ValuePropertyName,
         TypePropertyName  = TypePropertyName,
         AliasTypes        = false
     };
 }
Пример #11
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (!type.IsNullable())
            {
                handler = null;
                return(false);
            }

            var actualType     = type.GetGenericArguments().Single();
            var contentIsProps = container.GetHandler(actualType).ContentIsProperties;
            var constructor    = typeof(NullableTrwHandler <>).MakeGenericType(actualType).GetConstructor(new [] { typeof(bool) });

            Debug.Assert(constructor != null, "constructor != null");
            handler = (ITrwSerializationHandler)constructor.Invoke(new object[] { contentIsProps });
            return(true);
        }
Пример #12
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            var listInterface = type.GetAllInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList <>));

            if (listInterface == null)
            {
                handler = null;
                return(false);
            }

            var itemType = listInterface.GenericTypeArguments[0];
            var ctor     = typeof(ListTrwHandler <>).MakeGenericType(itemType).GetConstructor(Type.EmptyTypes);

            Debug.Assert(ctor != null, nameof(ctor) + " != null");
            handler = (ITrwSerializationHandler)ctor.Invoke(EmptyArrays <object> .Array);
            return(true);
        }
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            // todo: separate to IDictionary and IEnumerable<KVP>

            handler = null;

            var enumType = type.GetInterfaces()
                           .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>));

            if (enumType == null)
            {
                return(false);
            }

            var enumArgType = enumType.GetGenericArguments().Single();

            if (!enumArgType.IsGenericType || enumArgType.GetGenericTypeDefinition() != typeof(KeyValuePair <,>))
            {
                return(false);
            }

            var pairTypeGenericArguments = enumArgType.GetGenericArguments();
            var keyType   = pairTypeGenericArguments[0];
            var valueType = pairTypeGenericArguments[1];

            if (keyType != typeof(string))
            {
                return(false);
            }

            if (!type.IsAssignableFrom(typeof(Dictionary <,>).MakeGenericType(keyType, valueType)))
            {
                return(false);
            }

            var ctor = typeof(StringDictionaryTrwHandler <,>).MakeGenericType(type, valueType).GetConstructor(Type.EmptyTypes);

            Debug.Assert(ctor != null, nameof(ctor) + " != null");
            handler = (ITrwSerializationHandler)ctor.Invoke(EmptyArrays <object> .Array);
            Debug.Assert(handler.Type == type, "handler.Type == type");
            return(true);
        }
Пример #14
0
 public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
 {
     handler = CreateHandlerOrNull(type);
     return(handler != null);
 }