private ITrwSerializationHandler CreateHandler(Type type)
        {
            ITrwSerializationHandler handler = null;

            return(families.Any(family => family.TryCreateHandlerFor(type, this, out handler))
                ? handler
                : throw new ArgumentException($"No serialization handler found for the type '{type}'."));
        }
        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);
        }
示例#3
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 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);
 }
示例#5
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);
        }
示例#6
0
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (typeof(IPropertyBag).IsAssignableFrom(type))
            {
                handler = new PropertyBagTrwHandler();
                return(true);
            }

            handler = null;
            return(false);
        }
 public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
 {
     handler = CreateHandlerOrNull(type);
     return(handler != null);
 }
示例#8
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);
        }
        public bool TryCreateHandlerFor(Type type, ITrwSerializationHandlerContainer container, out ITrwSerializationHandler handler)
        {
            if (typeof(GeneratedResourceSource).IsAssignableFrom(type))
            {
                handler = new GeneratedResourceSourceTrwHandler();
                return(true);
            }

            handler = null;
            return(false);
        }
        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);
        }
示例#11
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);
        }
示例#12
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);
        }
示例#13
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);
        }