示例#1
0
        internal T?ConvertNullable <T>(
            string prefix,
            string name,
            Items items,
            IEnumerable <Items> values,
            T?defaultValue)
            where T : struct
        {
            var pfx = prefix + name;

            if (!items.TryGetValue(pfx, out var value) || value == null)
            {
                return(defaultValue);
            }

            if (TypeConverters.TryGetValue(pfx, out var converter))
            {
                return(converter.Convert <T>(value));
            }

            converter = Options.Converters.First(x => x.CanConvert <T>(value));
            TypeConverters.Add(pfx, converter);

            return(converter.Convert <T>(value));
        }
示例#2
0
 public static void RegisterTypeConverters(params IConvertType[] converters)
 {
     lock (TypeConverters)
     {
         foreach (var converter in converters)
         {
             TypeConverters.Add(converter);
         }
     }
 }
示例#3
0
        private static void LoadTypeConverters(Assembly assem)
        {
            if (TypeConverters == null)
            {
                TypeConverters = new Dictionary <Type, IList <TypeConverter> >();
            }

            var tcType = typeof(TypeConverter);

            var types = LoadedTypes[assem].Where(t => tcType.IsAssignableFrom(t) && t != tcType).ToList();

            foreach (var t in types)
            {
                var tc = Activator.CreateInstance(t) as TypeConverter;

                if (!TypeConverters.ContainsKey(tc.FromType))
                {
                    TypeConverters.Add(tc.FromType, new List <TypeConverter>());
                }

                TypeConverters[tc.FromType].Add(tc);
            }
        }
示例#4
0
        internal T Convert <T>(
            string prefix,
            string name,
            Items items,
            List <Items> values,
            T defaultValue)
        {
            var pfx = prefix + name;

            if (!items.TryGetValue(pfx, out var value) || value == null)
            {
                return(defaultValue);
            }

            if (TypeConverters.TryGetValue(pfx, out var converter))
            {
                return(converter.Convert <T>(value));
            }

            converter = Options.Converters.First(x => x.CanConvert <T>(value));
            TypeConverters.Add(pfx, converter);

            return(converter.Convert <T>(value));
        }
示例#5
0
        public virtual void InitTypeConverters()
        {
            bool useComplex = BaseTypeSequence.Contains(typeof(Complex));
            bool useBigInt  = BaseTypeSequence.Contains(typeof(BigInteger));
            //->string
            Type T = typeof(string);

            foreach (Type t in BaseTypeSequence)
            {
                if (t != T)
                {
                    TypeConverters.Add(t, T, ConvertAnyToString);
                }
            }
            //->Complex
            if (useComplex)
            {
                TypeConverters.Add(typeof(sbyte), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(byte), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(Int16), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(UInt16), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(Int32), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(UInt32), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(Int64), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(UInt64), typeof(Complex), ConvertAnyToComplex);
                TypeConverters.Add(typeof(Single), typeof(Complex), ConvertAnyToComplex);
                if (useBigInt)
                {
                    TypeConverters.Add(typeof(BigInteger), typeof(Complex), ConvertBigIntToComplex);
                }
            }
            //->BigInteger
            if (useBigInt)
            {
                TypeConverters.Add(typeof(sbyte), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(byte), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(Int16), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(UInt16), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(Int32), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(UInt32), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(Int64), typeof(BigInteger), ConvertAnyIntToBigInteger);
                TypeConverters.Add(typeof(UInt64), typeof(BigInteger), ConvertAnyIntToBigInteger);
            }

            //->Double
            TypeConverters.Add(typeof(sbyte), typeof(double), value => (double)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(double), value => (double)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(double), value => (double)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(double), value => (double)(UInt16)value);
            TypeConverters.Add(typeof(Int32), typeof(double), value => (double)(Int32)value);
            TypeConverters.Add(typeof(UInt32), typeof(double), value => (double)(UInt32)value);
            TypeConverters.Add(typeof(Int64), typeof(double), value => (double)(Int64)value);
            TypeConverters.Add(typeof(UInt64), typeof(double), value => (double)(UInt64)value);
            TypeConverters.Add(typeof(Single), typeof(double), value => (double)(Single)value);
            if (useBigInt)
            {
                TypeConverters.Add(typeof(BigInteger), typeof(double), value => ((BigInteger)value).ToDouble(null));
            }
            //->Single
            TypeConverters.Add(typeof(sbyte), typeof(Single), value => (Single)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(Single), value => (Single)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(Single), value => (Single)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(Single), value => (Single)(UInt16)value);
            TypeConverters.Add(typeof(Int32), typeof(Single), value => (Single)(Int32)value);
            TypeConverters.Add(typeof(UInt32), typeof(Single), value => (Single)(UInt32)value);
            TypeConverters.Add(typeof(Int64), typeof(Single), value => (Single)(Int64)value);
            TypeConverters.Add(typeof(UInt64), typeof(Single), value => (Single)(UInt64)value);
            if (useBigInt)
            {
                TypeConverters.Add(typeof(BigInteger), typeof(Single), value => (Single)((BigInteger)value).ToDouble(null));
            }

            //->UInt64
            TypeConverters.Add(typeof(sbyte), typeof(UInt64), value => (UInt64)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(UInt64), value => (UInt64)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(UInt64), value => (UInt64)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(UInt64), value => (UInt64)(UInt16)value);
            TypeConverters.Add(typeof(Int32), typeof(UInt64), value => (UInt64)(Int32)value);
            TypeConverters.Add(typeof(UInt32), typeof(UInt64), value => (UInt64)(UInt32)value);
            TypeConverters.Add(typeof(Int64), typeof(UInt64), value => (UInt64)(Int64)value);
            //->Int64
            TypeConverters.Add(typeof(sbyte), typeof(Int64), value => (Int64)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(Int64), value => (Int64)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(Int64), value => (Int64)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(Int64), value => (Int64)(UInt16)value);
            TypeConverters.Add(typeof(Int32), typeof(Int64), value => (Int64)(Int32)value);
            TypeConverters.Add(typeof(UInt32), typeof(Int64), value => (Int64)(UInt32)value);
            //->UInt32
            TypeConverters.Add(typeof(sbyte), typeof(UInt32), value => (UInt32)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(UInt32), value => (UInt32)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(UInt32), value => (UInt32)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(UInt32), value => (UInt32)(UInt16)value);
            TypeConverters.Add(typeof(Int32), typeof(UInt32), value => (UInt32)(Int32)value);
            //->Int32
            TypeConverters.Add(typeof(sbyte), typeof(Int32), value => (Int32)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(Int32), value => (Int32)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(Int32), value => (Int32)(Int16)value);
            TypeConverters.Add(typeof(UInt16), typeof(Int32), value => (Int32)(UInt16)value);
            //->UInt16
            TypeConverters.Add(typeof(sbyte), typeof(UInt16), value => (UInt16)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(UInt16), value => (UInt16)(byte)value);
            TypeConverters.Add(typeof(Int16), typeof(UInt16), value => (UInt16)(Int16)value);
            //->Int16
            TypeConverters.Add(typeof(sbyte), typeof(Int16), value => (Int16)(sbyte)value);
            TypeConverters.Add(typeof(byte), typeof(Int16), value => (Int16)(byte)value);
            //->byte
            TypeConverters.Add(typeof(sbyte), typeof(byte), value => (byte)(sbyte)value);
        }