public void Deserialize_When2CtorsWithSameArgCount_EvaluateCorrectOne()
 {
     Serializer s = new Serializer(typeof(CtorMock));
     ObjectExpression expr = new ObjectExpression();
     expr.ResultType = typeof(CtorMock);
     NumericExpression IDExpr = new NumericExpression("10");
     ValueExpression StrExpr = new ValueExpression("name");
     expr.ConstructorArguments.Add(IDExpr);
     expr.ConstructorArguments.Add(StrExpr);
     CtorArgTypeResolver resolver = new CtorArgTypeResolver(expr,s.Config);
     Type[] argTypes = resolver.ResolveTypes();            
     CollectionAssert.AreElementsEqual(new Type[] { typeof(int), typeof(string) }, argTypes);
 }
Пример #2
0
        public void Deserialize_When2CtorsWithSameArgCount_EvaluateCorrectOne()
        {
            Serializer       s    = new Serializer();
            ObjectExpression expr = new ObjectExpression();

            expr.ResultType = typeof(CtorMock);
            NumericExpression IDExpr  = new NumericExpression("10");
            ValueExpression   StrExpr = new ValueExpression("name");

            expr.ConstructorArguments.Add(IDExpr);
            expr.ConstructorArguments.Add(StrExpr);
            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expr, s.Settings);

            Type[] argTypes = resolver.ResolveTypes();
            CollectionAssert.AreElementsEqual(new Type[] { typeof(int), typeof(string) }, argTypes);
        }
Пример #3
0
        /// <summary>
        /// Resolves and updates the types of any constructor arguments
        /// </summary>
        /// <param name="context">serialization context</param>
        /// <param name="expression">object expression</param>
        protected static void ResolveConstructorTypes(ISerializerSettings settings, ObjectExpression expression)
        {
            ITypeData handler = settings.Types[expression.ResultType];

            Type[] definedTypes = GetConstructorParameterTypes(handler.ConstructorParameters);

            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expression, settings, definedTypes);

            Type[] resolvedTypes = resolver.ResolveTypes();
            for (int i = 0; i < resolvedTypes.Length; i++)
            {
                if (resolvedTypes[i] != null)
                {
                    expression.ConstructorArguments[i].ResultType = resolvedTypes[i];
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Resolves and updates the types of any constructor arguments
        /// </summary>
        /// <param name="context">serialization context</param>
        /// <param name="expression">object expression</param>
        protected static void ResolveConstructorTypes(SerializationContext context, ObjectExpression expression)
        {
            TypeData handler = context.GetTypeHandler(expression.ResultType);

            Type[] definedTypes = GetConstructorParameterTypes(handler.ConstructorParameters);

            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expression, context, definedTypes);

            Type[] resolvedTypes = resolver.ResolveTypes();
            for (int i = 0; i < resolvedTypes.Length; i++)
            {
                if (resolvedTypes[i] != null)
                {
                    expression.ConstructorArguments[i].ResultType = resolvedTypes[i];
                }
            }
        }
        public void Deserialize_UseCorrectTypes_WhenParametersDefined()
        {
            Serializer s = new Serializer(typeof(CtorMock2));
            ObjectExpression expr = new ObjectExpression();
            expr.ResultType = typeof(CtorMock2);
            NumericExpression IDExpr = new NumericExpression("10");
            ObjectExpression objExpr = new ObjectExpression();
            objExpr.ConstructorArguments.Add(new ValueExpression("name"));
            expr.ConstructorArguments.Add(IDExpr);
            expr.ConstructorArguments.Add(objExpr);
            Type[] definedTypes = new Type[] { typeof(int), typeof(MyObject2) };
            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expr, s.Config, definedTypes);
            Type[] argTypes = resolver.ResolveTypes();
            CollectionAssert.AreElementsEqual(new Type[] { typeof(long), typeof(MyObject2) }, argTypes);

            // Try to construct
            IDExpr.ResultType = typeof(long);
            objExpr.ResultType = typeof(MyObject2);
            Evaluator eval = new Evaluator(s.Config);
            object result = eval.Evaluate(expr);            
        }
Пример #6
0
        public void Deserialize_UseCorrectTypes_WhenParametersDefined()
        {
            Serializer       s    = new Serializer();
            ObjectExpression expr = new ObjectExpression();

            expr.ResultType = typeof(CtorMock2);
            NumericExpression IDExpr  = new NumericExpression("10");
            ObjectExpression  objExpr = new ObjectExpression();

            objExpr.ConstructorArguments.Add(new ValueExpression("name"));
            expr.ConstructorArguments.Add(IDExpr);
            expr.ConstructorArguments.Add(objExpr);
            Type[] definedTypes          = new Type[] { typeof(int), typeof(MyObject2) };
            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expr, s.Settings, definedTypes);

            Type[] argTypes = resolver.ResolveTypes();
            CollectionAssert.AreElementsEqual(new Type[] { typeof(long), typeof(MyObject2) }, argTypes);

            // Try to construct
            IDExpr.ResultType  = typeof(long);
            objExpr.ResultType = typeof(MyObject2);
            Evaluator eval   = new Evaluator(s.Settings);
            object    result = eval.Evaluate(expr);
        }
Пример #7
0
        /// <summary>
        /// Resolves and updates the types of any constructor arguments
        /// </summary>
        /// <param name="context">serialization context</param>
        /// <param name="expression">object expression</param>
        protected static void ResolveConstructorTypes(IConfiguration config, ObjectExpression expression)
        {
            TypeData handler = config.GetTypeHandler(expression.ResultType);
            Type[] definedTypes = GetConstructorParameterTypes(handler.ConstructorParameters);

            CtorArgTypeResolver resolver = new CtorArgTypeResolver(expression, config, definedTypes);
            Type[] resolvedTypes = resolver.ResolveTypes();
            for (int i = 0; i < resolvedTypes.Length; i++)
            {
                if (resolvedTypes[i] != null)
                    expression.ConstructorArguments[i].ResultType = resolvedTypes[i];
            }
        }