Пример #1
0
        static void TestExtension()
        {
            Int2String zip = Zipper;

            WriteLine($"\n<----- Extension Test ----->\nint\tZip Code");
            int zipNum = 1;

            Display(zipNum, zip); zipNum += 10;
            Display(zipNum, zip); zipNum += 100;
            Display(zipNum, zip); zipNum += 1000;
            Display(zipNum, zip); zipNum += 10000;
            Display(zipNum, zip); zipNum += 100000;
            Display(zipNum, zip);
        }
Пример #2
0
 private static void Display(int zip, Int2String zipCode)
 {
     WriteLine($"{zip}\t{zipCode(zip)}");
 }
Пример #3
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 !);
        }