public void TransformTest()
 {
     string[] questions = { "0", "1", "5", "100", "999", "1234567890" };
     int[]    answers   = { 0, 1, 5, 100, 999, 1234567890 };
     for (int testNumber = 1; testNumber <= answers.Length; ++testNumber)
     {
         Assert.Equals(String2Int.Transform(questions[testNumber]), answers[testNumber]);
     }
 }
Esempio n. 2
0
        internal static object createConverterInstance(Type tSource, Type tDest)
        {
            object?instance              = null;
            Type?  converterType         = null;
            bool   converterHasSourceArg = false;

            if (tSource == tDest)
            {
                converterType = typeof(Identity <>);
            }
            else if (tSource == typeof(int))
            {
                if (tDest == typeof(uint))
                {
                    instance = new Int2Uint();
                }
                else if (tDest == typeof(double))
                {
                    instance = new Int2Number();
                }
                else if (tDest == typeof(string))
                {
                    instance = new Int2String();
                }
                else if (tDest == typeof(bool))
                {
                    instance = new Int2Boolean();
                }
                else if (tDest == typeof(ASAny))
                {
                    instance = new Int2Any();
                }
                else if (tDest == typeof(ASObject))
                {
                    instance = new Int2Object();
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource == typeof(uint))
            {
                if (tDest == typeof(int))
                {
                    instance = new Uint2Int();
                }
                else if (tDest == typeof(double))
                {
                    instance = new Uint2Number();
                }
                else if (tDest == typeof(string))
                {
                    instance = new Uint2String();
                }
                else if (tDest == typeof(bool))
                {
                    instance = new Uint2Boolean();
                }
                else if (tDest == typeof(ASAny))
                {
                    instance = new Uint2Any();
                }
                else if (tDest == typeof(ASObject))
                {
                    instance = new Uint2Object();
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource == typeof(double))
            {
                if (tDest == typeof(int))
                {
                    instance = new Number2Int();
                }
                else if (tDest == typeof(uint))
                {
                    instance = new Number2Uint();
                }
                else if (tDest == typeof(string))
                {
                    instance = new Number2String();
                }
                else if (tDest == typeof(bool))
                {
                    instance = new Number2Boolean();
                }
                else if (tDest == typeof(ASAny))
                {
                    instance = new Number2Any();
                }
                else if (tDest == typeof(ASObject))
                {
                    instance = new Number2Object();
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource == typeof(string))
            {
                if (tDest == typeof(int))
                {
                    instance = new String2Int();
                }
                else if (tDest == typeof(uint))
                {
                    instance = new String2Uint();
                }
                else if (tDest == typeof(double))
                {
                    instance = new String2Number();
                }
                else if (tDest == typeof(bool))
                {
                    instance = new String2Boolean();
                }
                else if (tDest == typeof(ASAny))
                {
                    instance = new String2Any();
                }
                else if (tDest == typeof(ASObject))
                {
                    instance = new String2Object();
                }
                else if (!tDest.IsValueType)
                {
                    converterType = typeof(InvalidExceptNull <,>);
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource == typeof(bool))
            {
                if (tDest == typeof(int))
                {
                    instance = new Boolean2Int();
                }
                else if (tDest == typeof(uint))
                {
                    instance = new Boolean2Uint();
                }
                else if (tDest == typeof(double))
                {
                    instance = new Boolean2Number();
                }
                else if (tDest == typeof(string))
                {
                    instance = new Boolean2String();
                }
                else if (tDest == typeof(ASAny))
                {
                    instance = new Boolean2Any();
                }
                else if (tDest == typeof(ASObject))
                {
                    instance = new Boolean2Object();
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource == typeof(ASAny))
            {
                if (tDest == typeof(int))
                {
                    instance = new Any2Int();
                }
                else if (tDest == typeof(uint))
                {
                    instance = new Any2Uint();
                }
                else if (tDest == typeof(double))
                {
                    instance = new Any2Number();
                }
                else if (tDest == typeof(string))
                {
                    instance = new Any2String();
                }
                else if (tDest == typeof(bool))
                {
                    instance = new Any2Boolean();
                }
                else if (!tDest.IsValueType)
                {
                    converterType = typeof(Any2Object <>);
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (tSource.IsInterface || typeof(ASObject).IsAssignableFrom(tSource))
            {
                if (tDest == typeof(int))
                {
                    converterType         = typeof(Object2Int <>);
                    converterHasSourceArg = true;
                }
                else if (tDest == typeof(uint))
                {
                    converterType         = typeof(Object2Uint <>);
                    converterHasSourceArg = true;
                }
                else if (tDest == typeof(double))
                {
                    converterType         = typeof(Object2Number <>);
                    converterHasSourceArg = true;
                }
                else if (tDest == typeof(string))
                {
                    converterType         = typeof(Object2String <>);
                    converterHasSourceArg = true;
                }
                else if (tDest == typeof(bool))
                {
                    converterType         = typeof(Object2Boolean <>);
                    converterHasSourceArg = true;
                }
                else if (tDest == typeof(ASAny))
                {
                    converterType         = typeof(Object2Any <>);
                    converterHasSourceArg = true;
                }
                else if (!tDest.IsValueType)
                {
                    converterType = typeof(Object2Object <,>);
                }
                else
                {
                    converterType = typeof(Invalid <,>);
                }
            }
            else if (!tSource.IsValueType && !tDest.IsValueType)
            {
                converterType = typeof(Object2Object <,>);
            }
            else
            {
                converterType = typeof(Invalid <,>);
            }

            if (converterType != null)
            {
                if (converterType.GetGenericArguments().Length == 2)
                {
                    instance = Activator.CreateInstance(converterType.MakeGenericType(tSource, tDest));
                }
                else if (converterHasSourceArg)
                {
                    instance = Activator.CreateInstance(converterType.MakeGenericType(tSource));
                }
                else
                {
                    instance = Activator.CreateInstance(converterType.MakeGenericType(tDest));
                }
            }

            return(instance !);
        }