public void ConvertTest()
        {
            var value = SafeConverter.Convert(0, typeof(bool));

            Assert.AreEqual(value.GetType(), typeof(bool));
            Assert.AreEqual(value.ToString(), "False");

            Assert.AreEqual(SafeConverter.Convert <bool>(0), false);
            Assert.AreEqual(SafeConverter.Convert <bool>("0"), false);
            Assert.AreEqual(SafeConverter.Convert <bool>("1"), true);

            Assert.AreEqual(SafeConverter.Convert <int>("1"), 1);

            var separator     = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            var stringDecimal = "1,89".Replace(".", separator).Replace(",", separator);

            Assert.AreEqual(SafeConverter.Convert <double>(stringDecimal), 1.89d);

            value = SafeConverter.Convert(0, typeof(string));
            Assert.AreEqual(value.GetType(), typeof(string));

            Assert.AreEqual(SafeConverter.Convert <int?>("1"), 1);
            Assert.AreEqual(SafeConverter.Convert <int?>(null), null);
            Assert.AreEqual(SafeConverter.Convert <int?>(""), null);
            Assert.AreEqual(SafeConverter.Convert(null, typeof(int?)), null);

            Assert.AreEqual(SafeConverter.Convert <int>(BindingFlags.Static), 8);
            Assert.AreEqual(SafeConverter.Convert <BindingFlags>(8), BindingFlags.Static);
            Assert.AreEqual(SafeConverter.Convert <BindingFlags>("8"), BindingFlags.Static);
        }
Esempio n. 2
0
        public float GetFloat(int i)
        {
            var value = GetValue(i);

            if (value != null)
            {
                float num;
                if (float.TryParse(value.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out num))
                {
                    return(num);
                }
            }
            return(SafeConverter.Convert <float>(value));
        }
Esempio n. 3
0
        public double GetDouble(int i)
        {
            var value = GetValue(i);

            if (value != null)
            {
                double num;
                if (double.TryParse(value.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out num))
                {
                    return(num);
                }
            }
            return(SafeConverter.Convert <double>(value));
        }
Esempio n. 4
0
 public string GetString(int i)
 {
     return(SafeConverter.Convert <string>(GetValue(i)));
 }
Esempio n. 5
0
 public long GetInt64(int i)
 {
     return(SafeConverter.Convert <long>(GetValue(i)));
 }
Esempio n. 6
0
 public int GetInt32(int i)
 {
     return(SafeConverter.Convert <int>(GetValue(i)));
 }
Esempio n. 7
0
 public short GetInt16(int i)
 {
     return(SafeConverter.Convert <short>(GetValue(i)));
 }
Esempio n. 8
0
 public Guid GetGuid(int i)
 {
     return(SafeConverter.Convert <Guid>(GetValue(i)));
 }
Esempio n. 9
0
 public char GetChar(int i)
 {
     return(SafeConverter.Convert <char>(GetValue(i)));
 }
Esempio n. 10
0
 public byte GetByte(int i)
 {
     return(SafeConverter.Convert <byte>(GetValue(i)));
 }
Esempio n. 11
0
 public bool GetBoolean(int i)
 {
     return(SafeConverter.Convert <bool>(GetValue(i)));
 }