System.IConvertible.ToInt16(System.IFormatProvider)

Here are the examples of the csharp api System.IConvertible.ToInt16(System.IFormatProvider) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

50 Examples 7

19 Source : VBExtension.cs
with BSD 2-Clause "Simplified" License
from bhoogter

public static short CShort(object A) { short z = 0; return (A is System.IConvertible) ? ((System.IConvertible)A).ToInt16(null) : z; }

19 Source : CompressedString.cs
with MIT License
from circles-arrows

short IConvertible.ToInt16(IFormatProvider? provider)
        {
            return ((IConvertible)Value).ToInt16(provider);
        }

19 Source : JsonConvert.cs
with MIT License
from CragonGame

public static string ToString(object value)
    {
      if (value == null)
        return Null;

      IConvertible convertible = value as IConvertible;

      if (convertible != null)
      {
        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            return ToString(convertible.ToString(CultureInfo.InvariantCulture));
          case TypeCode.Char:
            return ToString(convertible.ToChar(CultureInfo.InvariantCulture));
          case TypeCode.Boolean:
            return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture));
          case TypeCode.SByte:
            return ToString(convertible.ToSByte(CultureInfo.InvariantCulture));
          case TypeCode.Int16:
            return ToString(convertible.ToInt16(CultureInfo.InvariantCulture));
          case TypeCode.UInt16:
            return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture));
          case TypeCode.Int32:
            return ToString(convertible.ToInt32(CultureInfo.InvariantCulture));
          case TypeCode.Byte:
            return ToString(convertible.ToByte(CultureInfo.InvariantCulture));
          case TypeCode.UInt32:
            return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture));
          case TypeCode.Int64:
            return ToString(convertible.ToInt64(CultureInfo.InvariantCulture));
          case TypeCode.UInt64:
            return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture));
          case TypeCode.Single:
            return ToString(convertible.ToSingle(CultureInfo.InvariantCulture));
          case TypeCode.Double:
            return ToString(convertible.ToDouble(CultureInfo.InvariantCulture));
          case TypeCode.DateTime:
            return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture));
          case TypeCode.Decimal:
            return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
          case TypeCode.DBNull:
            return Null;
        }
      }
      else if (value is DateTimeOffset)
      {
        return ToString((DateTimeOffset)value);
      }
      else if (value is Guid)
      {
        return ToString((Guid) value);
      }
      else if (value is Uri)
      {
        return ToString((Uri) value);
      }
      else if (value is TimeSpan)
      {
        return ToString((TimeSpan) value);
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : JsonWriter.cs
with MIT License
from CragonGame

public virtual void WriteValue(object value)
    {
      if (value == null)
      {
        WriteNull();
        return;
      }
      else if (value is IConvertible)
      {
        IConvertible convertible = value as IConvertible;

        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Char:
            WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Boolean:
            WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
            return;
          case TypeCode.SByte:
            WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int16:
            WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt16:
            WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int32:
            WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Byte:
            WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt32:
            WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int64:
            WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt64:
            WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Single:
            WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Double:
            WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DateTime:
            WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Decimal:
            WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DBNull:
            WriteNull();
            return;
        }
      }
      else if (value is DateTimeOffset)
      {
        WriteValue((DateTimeOffset)value);
        return;
      }
      else if (value is byte[])
      {
        WriteValue((byte[])value);
        return;
      }
      else if (value is Guid)
      {
        WriteValue((Guid)value);
        return;
      }
      else if (value is Uri)
      {
        WriteValue((Uri)value);
        return;
      }
      else if (value is TimeSpan)
      {
        WriteValue((TimeSpan)value);
        return;
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : SYSTEMTIME.cs
with MIT License
from dahall

short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)(DateTime)this).ToInt16(provider);

19 Source : BOOLEAN.cs
with MIT License
from dahall

short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider);

19 Source : HRESULT.cs
with MIT License
from dahall

short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider);

19 Source : Win32Error.cs
with MIT License
from dahall

short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)value).ToInt16(provider);

19 Source : NTStatusTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			NTStatus nts = NTStatus.STATUS_ACCESS_DENIED;
			var c = (IConvertible)nts;
			var cv = (IConvertible)NTStatus.STATUS_ACCESS_DENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(() => c.ToSByte(f), Throws.Exception);
			replacedert.That(() => c.ToByte(f), Throws.Exception);
			replacedert.That(() => c.ToInt16(f), Throws.Exception);
			replacedert.That(() => c.ToUInt16(f), Throws.Exception);
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToUInt32(f), Is.EqualTo((uint)nts));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo((ulong)(uint)nts));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(c.ToBoolean(f), Is.EqualTo(nts.Succeeded));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("STATUS_ACCESS_DENIED"));
			replacedert.That(c.ToType(typeof(int), f), Is.EqualTo(cv.ToType(typeof(int), f)));
		}

19 Source : HRESULTTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			HRESULT hr = HRESULT.E_ACCESSDENIED;
			var c = (IConvertible) hr;
			var cv = (IConvertible)HRESULT.E_ACCESSDENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(() => c.ToSByte(f), Throws.Exception);
			replacedert.That(() => c.ToByte(f), Throws.Exception);
			replacedert.That(() => c.ToInt16(f), Throws.Exception);
			replacedert.That(() => c.ToUInt16(f), Throws.Exception);
			replacedert.That(c.ToUInt32(f), Is.EqualTo(unchecked((uint)HRESULT.E_ACCESSDENIED)));
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo((ulong)unchecked((uint)HRESULT.E_ACCESSDENIED)));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("E_ACCESSDENIED"));
			replacedert.That(c.ToType(typeof(int), f), Is.EqualTo(cv.ToType(typeof(int), f)));
		}

19 Source : Win32ErrorTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			Win32Error err = Win32Error.ERROR_ACCESS_DENIED;
			var c = (IConvertible)err;
			var cv = (IConvertible)Win32Error.ERROR_ACCESS_DENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(c.ToSByte(f), Is.EqualTo(cv.ToSByte(f)));
			replacedert.That(c.ToByte(f), Is.EqualTo(cv.ToByte(f)));
			replacedert.That(c.ToInt16(f), Is.EqualTo(cv.ToInt16(f)));
			replacedert.That(c.ToUInt16(f), Is.EqualTo(cv.ToUInt16(f)));
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToUInt32(f), Is.EqualTo(cv.ToUInt32(f)));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo(cv.ToUInt64(f)));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("ERROR_ACCESS_DENIED"));
			replacedert.That(c.ToType(typeof(uint), f), Is.EqualTo(cv.ToType(typeof(uint), f)));
		}

19 Source : AseDataReader.cs
with Apache License 2.0
from DataAction

public override short GetInt16(int i)
        {
            return GetPrimitive(i, (value, provider) => value.ToInt16(provider));
        }

19 Source : SqlNative.cs
with MIT License
from DataObjects-NET

short IConvertible.ToInt16(IFormatProvider provider)
    {
      return ((IConvertible) Value).ToInt16(provider);
    }

19 Source : AbapStringValue.cs
with MIT License
from dbosoft

public short ToInt16(IFormatProvider provider)
        {
            return ((IConvertible) Value).ToInt16(CultureInfo.InvariantCulture);
        }

19 Source : AbapIntValue.cs
with MIT License
from dbosoft

public short ToInt16(IFormatProvider provider)
        {
            return ((IConvertible) Value).ToInt16(provider);
        }

19 Source : _Int16Enum_T.cs
with MIT License
from DevZest

private static Int16? PerformConvert(T? value)
        {
            if (!value.HasValue)
                return null;
            return value.Value.ToInt16(null);
        }

19 Source : OracleDataReader.cs
with MIT License
from ericmend

public
        override
        short GetInt16(int i)
        {
            IConvertible c = (IConvertible)GetValue(i);
            return c.ToInt16(CultureInfo.CurrentCulture);
        }

19 Source : Calculator.cs
with MIT License
from gimlichael

private static T CalculateCore<T>(T x, T y, replacedignmentOperator replacedignment) where T : struct, IConvertible
        {
            var provider = CultureInfo.InvariantCulture;
            var replacedignmentCode = x.GetTypeCode();
            switch (replacedignmentCode)
            {
                case TypeCode.Byte:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToByte(provider) + y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToByte(provider) & y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToByte(provider) / y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToByte(provider) ^ y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToByte(provider) << y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToByte(provider) * y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToByte(provider) | y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToByte(provider) % y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToByte(provider) >> y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToByte(provider) - y.ToByte(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Decimal:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) + y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) / y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) * y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) % y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) - y.ToDecimal(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Double:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToDouble(provider) + y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToDouble(provider) / y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToDouble(provider) * y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToDouble(provider) % y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToDouble(provider) - y.ToDouble(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int16:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt16(provider) + y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt16(provider) & y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt16(provider) / y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt16(provider) ^ y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToInt16(provider) << y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt16(provider) * y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt16(provider) | y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt16(provider) % y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToInt16(provider) >> y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt16(provider) - y.ToInt16(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int32:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt32(provider) + y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt32(provider) & y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt32(provider) / y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt32(provider) ^ y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToInt32(provider) << y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt32(provider) * y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt32(provider) | y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt32(provider) % y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToInt32(provider) >> y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt32(provider) - y.ToInt32(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int64:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt64(provider) + y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt64(provider) & y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt64(provider) / y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt64(provider) ^ y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt64(provider) * y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt64(provider) | y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt64(provider) % y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt64(provider) - y.ToInt64(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.SByte:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToSByte(provider) + y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToSByte(provider) & y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToSByte(provider) / y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToSByte(provider) ^ y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToSByte(provider) << y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToSByte(provider) * y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToSByte(provider) | y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToSByte(provider) % y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToSByte(provider) >> y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToSByte(provider) - y.ToSByte(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Single:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToSingle(provider) + y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToSingle(provider) / y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToSingle(provider) * y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToSingle(provider) % y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToSingle(provider) - y.ToSingle(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt16:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) + y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) & y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) / y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) ^ y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) << y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) * y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) | y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) % y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) >> y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) - y.ToUInt16(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt32:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) + y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) & y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) / y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) ^ y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) * y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) | y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) % y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) - y.ToUInt32(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt64:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) + y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) & y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) / y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) ^ y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) * y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) | y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) % y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) - y.ToUInt64(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                default:
                    throw new TypeArgumentException("T", string.Format(CultureInfo.InvariantCulture, "T appears to contain an invalid type. Expected type is numeric and must be one of the following: Byte, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32 or UInt64. Actually type was {0}.", typeof(T).Name));
            }
        }

19 Source : Convertible.cs
with MIT License
from gimlichael

public static byte[] GetBytes(Enum input, Action<EndianOptions> setup = null)
        {
            var tc = input.GetTypeCode();
            var c = input as IConvertible;
            switch (tc)
            {
                case TypeCode.Byte:
                {
                    return GetBytes(c.ToByte(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.Int16:
                {
                    return GetBytes(c.ToInt16(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.Int64:
                {
                    return GetBytes(c.ToInt64(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.UInt16:
                {
                    return GetBytes(c.ToUInt16(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.UInt32:
                {
                    return GetBytes(c.ToUInt32(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.UInt64:
                {
                    return GetBytes(c.ToUInt64(CultureInfo.InvariantCulture), setup);
                }
                case TypeCode.SByte:
                {
                    return GetBytes(c.ToSByte(CultureInfo.InvariantCulture), setup);
                }
                default:
                    return GetBytes(c.ToInt32(CultureInfo.InvariantCulture), setup);
            }
        }

19 Source : bool.cs
with MIT License
from GridProtectionAlliance

short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)Value).ToInt16(provider);

19 Source : string.cs
with MIT License
from GridProtectionAlliance

short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)ToString()).ToInt16(provider);

19 Source : complex64.cs
with MIT License
from GridProtectionAlliance

short IConvertible.ToInt16(IFormatProvider? provider) => ((IConvertible)m_real).ToInt16(provider);

19 Source : ServiceElement.cs
with MIT License
from inthehand

private static object ConvertNumericalValue(ElementType elementType, object value)
        {
            object naturalTypedValue = null;
            Exception innerEx;
            //
            if (value is byte || value is Int16 || value is Int32 || value is Int64
                || value is sbyte || value is UInt16 || value is UInt32 || value is UInt64
                || value is Enum) {
                try {
                    IConvertible cble = (IConvertible)value;
                    IFormatProvider fp = System.Globalization.CultureInfo.InvariantCulture;
                    switch (elementType) {
                        case ElementType.UInt8:
                            naturalTypedValue = cble.ToByte(fp);
                            break;
                        case ElementType.Int8:
                            naturalTypedValue = cble.ToSByte(fp);
                            break;
                        //--
                        case ElementType.UInt16:
                            naturalTypedValue = cble.ToUInt16(fp);
                            break;
                        case ElementType.Int16:
                            naturalTypedValue = cble.ToInt16(fp);
                            break;
                        //--
                        case ElementType.UInt64:
                            naturalTypedValue = cble.ToUInt64(fp);
                            break;
                        case ElementType.Int64:
                            naturalTypedValue = cble.ToInt64(fp);
                            break;
                        //--
                        case ElementType.UInt32:
                            naturalTypedValue = cble.ToUInt32(fp);
                            break;
                        default:
                            //case ElementType.Int32:
                            System.Diagnostics.Debug.replacedert(elementType == ElementType.Int32, "Unexpected numeric type");
                            naturalTypedValue = cble.ToInt32(fp);
                            break;
                    }
                    return naturalTypedValue;
                } catch (OverflowException ex) {
                    innerEx = ex;
                    //} catch (InvalidCastException ex) {
                    //    innerEx = ex;
                }
            } else {
                innerEx = null;
            }
            throw ServiceRecordParser.new_ArgumentOutOfRangeException(
                string.Format(System.Globalization.CultureInfo.InvariantCulture,
                    "Value '{1}'  of type '{2}' not valid for element type {0}.",
                    elementType, value, value.GetType()), innerEx);
        }

19 Source : ServiceElement.cs
with MIT License
from inthehand

private static object ConvertNumericalValue(ElementType elementType, object value)
        {
            object naturalTypedValue = null;
            Exception innerEx;
            //
            if (value is Byte || value is Int16 || value is Int32 || value is Int64
                || value is SByte || value is UInt16 || value is UInt32 || value is UInt64
                || value is Enum) {
                try {
                    IConvertible cble = (IConvertible)value;
                    IFormatProvider fp = System.Globalization.CultureInfo.InvariantCulture;
                    switch (elementType) {
                        case ElementType.UInt8:
                            naturalTypedValue = cble.ToByte(fp);
                            break;
                        case ElementType.Int8:
                            naturalTypedValue = cble.ToSByte(fp);
                            break;
                        //--
                        case ElementType.UInt16:
                            naturalTypedValue = cble.ToUInt16(fp);
                            break;
                        case ElementType.Int16:
                            naturalTypedValue = cble.ToInt16(fp);
                            break;
                        //--
                        case ElementType.UInt64:
                            naturalTypedValue = cble.ToUInt64(fp);
                            break;
                        case ElementType.Int64:
                            naturalTypedValue = cble.ToInt64(fp);
                            break;
                        //--
                        case ElementType.UInt32:
                            naturalTypedValue = cble.ToUInt32(fp);
                            break;
                        default:
                            //case ElementType.Int32:
                            System.Diagnostics.Debug.replacedert(elementType == ElementType.Int32, "Unexpected numeric type");
                            naturalTypedValue = cble.ToInt32(fp);
                            break;
                    }
                    return naturalTypedValue;
                } catch (OverflowException ex) {
                    innerEx = ex;
                    //} catch (InvalidCastException ex) {
                    //    innerEx = ex;
                }
            } else {
                innerEx = null;
            }
            throw ServiceRecordParser.new_ArgumentOutOfRangeException(
                String.Format(System.Globalization.CultureInfo.InvariantCulture,
                    "Value '{1}'  of type '{2}' not valid for element type {0}.",
                    elementType, value, value.GetType()), innerEx);
        }

19 Source : JsonConvert.cs
with Apache License 2.0
from intuit

public static string ToString(object value)
    {
      if (value == null)
        return Null;

      IConvertible convertible = value as IConvertible;

      if (convertible != null)
      {
        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            return ToString(convertible.ToString(CultureInfo.InvariantCulture));
          case TypeCode.Char:
            return ToString(convertible.ToChar(CultureInfo.InvariantCulture));
          case TypeCode.Boolean:
            return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture));
          case TypeCode.SByte:
            return ToString(convertible.ToSByte(CultureInfo.InvariantCulture));
          case TypeCode.Int16:
            return ToString(convertible.ToInt16(CultureInfo.InvariantCulture));
          case TypeCode.UInt16:
            return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture));
          case TypeCode.Int32:
            return ToString(convertible.ToInt32(CultureInfo.InvariantCulture));
          case TypeCode.Byte:
            return ToString(convertible.ToByte(CultureInfo.InvariantCulture));
          case TypeCode.UInt32:
            return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture));
          case TypeCode.Int64:
            return ToString(convertible.ToInt64(CultureInfo.InvariantCulture));
          case TypeCode.UInt64:
            return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture));
          case TypeCode.Single:
            return ToString(convertible.ToSingle(CultureInfo.InvariantCulture));
          case TypeCode.Double:
            return ToString(convertible.ToDouble(CultureInfo.InvariantCulture));
          case TypeCode.DateTime:
            return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture));
          case TypeCode.Decimal:
            return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
          case TypeCode.DBNull:
            return Null;
        }
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        return ToString((DateTimeOffset) value);
      }
#endif
      else if (value is Guid)
      {
        return ToString((Guid) value);
      }
      else if (value is Uri)
      {
        return ToString((Uri) value);
      }
      else if (value is TimeSpan)
      {
        return ToString((TimeSpan) value);
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : JsonWriter.cs
with Apache License 2.0
from intuit

public virtual void WriteValue(object value)
    {
      if (value == null)
      {
        WriteNull();
        return;
      }
      else if (value is IConvertible)
      {
        IConvertible convertible = value as IConvertible;

        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Char:
            WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Boolean:
            WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
            return;
          case TypeCode.SByte:
            WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int16:
            WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt16:
            WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int32:
            WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Byte:
            WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt32:
            WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int64:
            WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt64:
            WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Single:
            WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Double:
            WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DateTime:
            WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Decimal:
            WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DBNull:
            WriteNull();
            return;
        }
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        WriteValue((DateTimeOffset)value);
        return;
      }
#endif
      else if (value is byte[])
      {
        WriteValue((byte[])value);
        return;
      }
      else if (value is Guid)
      {
        WriteValue((Guid)value);
        return;
      }
      else if (value is Uri)
      {
        WriteValue((Uri)value);
        return;
      }
      else if (value is TimeSpan)
      {
        WriteValue((TimeSpan)value);
        return;
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : ReflectionWriter.cs
with Apache License 2.0
from leinlin

byte [] EncodeConstant (ElementType et, object value)
		{
			m_constWriter.Empty ();

			if (value == null)
				et = ElementType.Clreplaced;

			IConvertible ic = value as IConvertible;
			IFormatProvider fp = CultureInfo.CurrentCulture.NumberFormat;

			switch (et) {
			case ElementType.Boolean :
				m_constWriter.Write ((byte) (ic.ToBoolean (fp) ? 1 : 0));
				break;
			case ElementType.Char :
				m_constWriter.Write ((ushort) ic.ToChar (fp));
				break;
			case ElementType.I1 :
				m_constWriter.Write (ic.ToSByte (fp));
				break;
			case ElementType.I2 :
				m_constWriter.Write (ic.ToInt16 (fp));
				break;
			case ElementType.I4 :
				m_constWriter.Write (ic.ToInt32 (fp));
				break;
			case ElementType.I8 :
				m_constWriter.Write (ic.ToInt64 (fp));
				break;
			case ElementType.U1 :
				m_constWriter.Write (ic.ToByte (fp));
				break;
			case ElementType.U2 :
				m_constWriter.Write (ic.ToUInt16 (fp));
				break;
			case ElementType.U4 :
				m_constWriter.Write (ic.ToUInt32 (fp));
				break;
			case ElementType.U8 :
				m_constWriter.Write (ic.ToUInt64 (fp));
				break;
			case ElementType.R4 :
				m_constWriter.Write (ic.ToSingle (fp));
				break;
			case ElementType.R8 :
				m_constWriter.Write (ic.ToDouble (fp));
				break;
			case ElementType.String :
				m_constWriter.Write (Encoding.Unicode.GetBytes ((string) value));
				break;
			case ElementType.Clreplaced :
				m_constWriter.Write (new byte [4]);
				break;
			default :
				throw new ReflectionException ("Non valid element for a constant");
			}

			return m_constWriter.ToArray ();
		}

19 Source : PolymorphicEnum.cs
with GNU General Public License v3.0
from ME3Tweaks

short IConvertible.ToInt16(IFormatProvider provider)
		{
			return Ordinal.ToInt16(provider);
		}

19 Source : SevenBitNumber.cs
with MIT License
from melanchall

short IConvertible.ToInt16(IFormatProvider provider)
        {
            return ((IConvertible)_value).ToInt16(provider);
        }

19 Source : DefaultConverter.cs
with MIT License
from meziantou

protected virtual bool TryConvert(object? input, IFormatProvider? provider, out short value)
        {
            if (IsNullOrEmptyString(input))
            {
                value = 0;
                return false;
            }

            value = 0;
            if (input is byte[] inputBytes)
            {
                if (inputBytes.Length == 2)
                {
                    value = BitConverter.ToInt16(inputBytes, 0);
                    return true;
                }
                return false;
            }

            if (!(input is string))
            {
                if (input is IConvertible ic)
                {
                    try
                    {
                        value = ic.ToInt16(provider);
                        return true;
                    }
                    catch
                    {
                    }
                }
            }

            var styles = NumberStyles.Integer;
            var s = Convert.ToString(input, provider);
            if (NormalizeHexString(ref s))
            {
                styles |= NumberStyles.AllowHexSpecifier;
            }
            return short.TryParse(s, styles, provider, out value);
        }

19 Source : ColorString.cs
with MIT License
from microsoft

short IConvertible.ToInt16( IFormatProvider provider )
        {
            return ((IConvertible) this.ToString()).ToInt16( provider );
        }

19 Source : Number.cs
with Apache License 2.0
from NightOwl888

internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider? provider)
        {
            Debug.replacedert(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (ReferenceEquals(value!.GetType(), targetType))
            {
                return value;
            }

            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Boolean]))
                return value.ToBoolean(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Char]))
                return value.ToChar(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.SByte]))
                return value.ToSByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Byte]))
                return value.ToByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int16]))
                return value.ToInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt16]))
                return value.ToUInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int32]))
                return value.ToInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt32]))
                return value.ToUInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int64]))
                return value.ToInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt64]))
                return value.ToUInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Single]))
                return value.ToSingle(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Double]))
                return value.ToDouble(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Decimal]))
                return value.ToDecimal(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DateTime]))
                return value.ToDateTime(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.String]))
                return value.ToString(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Object]))
                return (object)value;
            // Need to special case Enum because typecode will be underlying type, e.g. Int32
            if (ReferenceEquals(targetType, EnumType))
                return (Enum)value;
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DBNull]))
                throw new InvalidCastException(SR.InvalidCast_DBNull);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Empty]))
                throw new InvalidCastException(SR.InvalidCast_Empty);

            throw new InvalidCastException(J2N.SR.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName));
        }

19 Source : BsonDecimal128.cs
with MIT License
from putifeng

protected override short IConvertibleToInt16Implementation(IFormatProvider provider)
        {
            return ((IConvertible)_value).ToInt16(provider);
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Scalar(object val, NPTypeCode typeCode)
        {
            switch (typeCode)
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#1>(UnmanagedMemoryBlock<#1>.FromPool(_buffer)) {[0] = ((IConvertible)val).To#1(CultureInfo.InvariantCulture)};
	            %
	            default:
		            throw new NotSupportedException();
#else

                case NPTypeCode.Boolean: return new ArraySlice<Boolean>(UnmanagedMemoryBlock<Boolean>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToBoolean(CultureInfo.InvariantCulture)};
                case NPTypeCode.Byte: return new ArraySlice<Byte>(UnmanagedMemoryBlock<Byte>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToByte(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int16: return new ArraySlice<Int16>(UnmanagedMemoryBlock<Int16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt16: return new ArraySlice<UInt16>(UnmanagedMemoryBlock<UInt16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int32: return new ArraySlice<Int32>(UnmanagedMemoryBlock<Int32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt32: return new ArraySlice<UInt32>(UnmanagedMemoryBlock<UInt32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int64: return new ArraySlice<Int64>(UnmanagedMemoryBlock<Int64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt64: return new ArraySlice<UInt64>(UnmanagedMemoryBlock<UInt64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.Char: return new ArraySlice<Char>(UnmanagedMemoryBlock<Char>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToChar(CultureInfo.InvariantCulture)};
                case NPTypeCode.Double: return new ArraySlice<Double>(UnmanagedMemoryBlock<Double>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDouble(CultureInfo.InvariantCulture)};
                case NPTypeCode.Single: return new ArraySlice<Single>(UnmanagedMemoryBlock<Single>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToSingle(CultureInfo.InvariantCulture)};
                case NPTypeCode.Decimal: return new ArraySlice<Decimal>(UnmanagedMemoryBlock<Decimal>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDecimal(CultureInfo.InvariantCulture)};
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Allocate(Type elementType, int count, object fill)
        {
            switch (elementType.GetTypeCode())
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#2>(new UnmanagedMemoryBlock<#2>(count, ((IConvertible)fill).To#1(CultureInfo.InvariantCulture)));
	            %
	            default:
		            throw new NotSupportedException();
#else
                case NPTypeCode.Boolean: return new ArraySlice<bool>(new UnmanagedMemoryBlock<bool>(count, ((IConvertible)fill).ToBoolean(CultureInfo.InvariantCulture)));
                case NPTypeCode.Byte: return new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>(count, ((IConvertible)fill).ToByte(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int16: return new ArraySlice<short>(new UnmanagedMemoryBlock<short>(count, ((IConvertible)fill).ToInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt16: return new ArraySlice<ushort>(new UnmanagedMemoryBlock<ushort>(count, ((IConvertible)fill).ToUInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int32: return new ArraySlice<int>(new UnmanagedMemoryBlock<int>(count, ((IConvertible)fill).ToInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt32: return new ArraySlice<uint>(new UnmanagedMemoryBlock<uint>(count, ((IConvertible)fill).ToUInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int64: return new ArraySlice<long>(new UnmanagedMemoryBlock<long>(count, ((IConvertible)fill).ToInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt64: return new ArraySlice<ulong>(new UnmanagedMemoryBlock<ulong>(count, ((IConvertible)fill).ToUInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.Char: return new ArraySlice<char>(new UnmanagedMemoryBlock<char>(count, ((IConvertible)fill).ToChar(CultureInfo.InvariantCulture)));
                case NPTypeCode.Double: return new ArraySlice<double>(new UnmanagedMemoryBlock<double>(count, ((IConvertible)fill).ToDouble(CultureInfo.InvariantCulture)));
                case NPTypeCode.Single: return new ArraySlice<float>(new UnmanagedMemoryBlock<float>(count, ((IConvertible)fill).ToSingle(CultureInfo.InvariantCulture)));
                case NPTypeCode.Decimal: return new ArraySlice<decimal>(new UnmanagedMemoryBlock<decimal>(count, ((IConvertible)fill).ToDecimal(CultureInfo.InvariantCulture)));
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Scalar(object val)
        {
            switch (val.GetType().GetTypeCode())
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#1>(UnmanagedMemoryBlock<#1>.FromPool(_buffer)) {[0] = ((IConvertible)val).To#1(CultureInfo.InvariantCulture)};
	            %
	            default:
		            throw new NotSupportedException();
#else

                case NPTypeCode.Boolean: return new ArraySlice<Boolean>(UnmanagedMemoryBlock<Boolean>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToBoolean(CultureInfo.InvariantCulture)};
                case NPTypeCode.Byte: return new ArraySlice<Byte>(UnmanagedMemoryBlock<Byte>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToByte(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int16: return new ArraySlice<Int16>(UnmanagedMemoryBlock<Int16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt16: return new ArraySlice<UInt16>(UnmanagedMemoryBlock<UInt16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int32: return new ArraySlice<Int32>(UnmanagedMemoryBlock<Int32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt32: return new ArraySlice<UInt32>(UnmanagedMemoryBlock<UInt32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int64: return new ArraySlice<Int64>(UnmanagedMemoryBlock<Int64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt64: return new ArraySlice<UInt64>(UnmanagedMemoryBlock<UInt64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.Char: return new ArraySlice<Char>(UnmanagedMemoryBlock<Char>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToChar(CultureInfo.InvariantCulture)};
                case NPTypeCode.Double: return new ArraySlice<Double>(UnmanagedMemoryBlock<Double>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDouble(CultureInfo.InvariantCulture)};
                case NPTypeCode.Single: return new ArraySlice<Single>(UnmanagedMemoryBlock<Single>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToSingle(CultureInfo.InvariantCulture)};
                case NPTypeCode.Decimal: return new ArraySlice<Decimal>(UnmanagedMemoryBlock<Decimal>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDecimal(CultureInfo.InvariantCulture)};
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Allocate(NPTypeCode typeCode, int count, object fill)
        {
            switch (typeCode)
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#2>(new UnmanagedMemoryBlock<#2>(count, ((IConvertible)fill).To#1(CultureInfo.InvariantCulture)));
	            %
	            default:
		            throw new NotSupportedException();
#else
                case NPTypeCode.Boolean: return new ArraySlice<bool>(new UnmanagedMemoryBlock<bool>(count, ((IConvertible)fill).ToBoolean(CultureInfo.InvariantCulture)));
                case NPTypeCode.Byte: return new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>(count, ((IConvertible)fill).ToByte(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int16: return new ArraySlice<short>(new UnmanagedMemoryBlock<short>(count, ((IConvertible)fill).ToInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt16: return new ArraySlice<ushort>(new UnmanagedMemoryBlock<ushort>(count, ((IConvertible)fill).ToUInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int32: return new ArraySlice<int>(new UnmanagedMemoryBlock<int>(count, ((IConvertible)fill).ToInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt32: return new ArraySlice<uint>(new UnmanagedMemoryBlock<uint>(count, ((IConvertible)fill).ToUInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int64: return new ArraySlice<long>(new UnmanagedMemoryBlock<long>(count, ((IConvertible)fill).ToInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt64: return new ArraySlice<ulong>(new UnmanagedMemoryBlock<ulong>(count, ((IConvertible)fill).ToUInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.Char: return new ArraySlice<char>(new UnmanagedMemoryBlock<char>(count, ((IConvertible)fill).ToChar(CultureInfo.InvariantCulture)));
                case NPTypeCode.Double: return new ArraySlice<double>(new UnmanagedMemoryBlock<double>(count, ((IConvertible)fill).ToDouble(CultureInfo.InvariantCulture)));
                case NPTypeCode.Single: return new ArraySlice<float>(new UnmanagedMemoryBlock<float>(count, ((IConvertible)fill).ToSingle(CultureInfo.InvariantCulture)));
                case NPTypeCode.Decimal: return new ArraySlice<decimal>(new UnmanagedMemoryBlock<decimal>(count, ((IConvertible)fill).ToDecimal(CultureInfo.InvariantCulture)));
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : OpGT.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) > 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) > 0);
        }

19 Source : OpLE.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) <= 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) <= 0);
        }

19 Source : OpLT.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) < 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) < 0);
        }

19 Source : Operator.cs
with Apache License 2.0
from SteeltoeOSS

public static bool EqualityCheck(IEvaluationContext context, object left, object right)
        {
            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
            }

            if (left is string && right is string)
            {
                return left.Equals(right);
            }

            if (left is bool && right is bool)
            {
                return left.Equals(right);
            }

            if (ObjectUtils.NullSafeEquals(left, right))
            {
                return true;
            }

            if (left is IComparable && right is IComparable)
            {
                var ancestor = ClreplacedUtils.DetermineCommonAncestor(left.GetType(), right.GetType());
                if (ancestor != null && typeof(IComparable).IsreplacedignableFrom(ancestor))
                {
                    return context.TypeComparator.Compare(left, right) == 0;
                }
            }

            return false;
        }

19 Source : OpGE.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) >= 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) >= 0);
        }

19 Source : AppContextConfigHelper.cs
with MIT License
from Team-RTCLI

internal static short GetInt16Config(string configName, short defaultValue, bool allowNegative = true)
        {
            try
            {
                object? config = AppContext.GetData(configName);
                short result = defaultValue;
                switch (config)
                {
                    case uint value:
                        {
                            result = (short)value;
                            if ((uint)result != value)
                            {
                                return defaultValue; // overflow
                            }
                            break;
                        }
                    case string str:
                        if (str.StartsWith("0x"))
                        {
                            result = Convert.ToInt16(str, 16);
                        }
                        else if (str.StartsWith("0"))
                        {
                            result = Convert.ToInt16(str, 8);
                        }
                        else
                        {
                            result = short.Parse(str, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo);
                        }
                        break;
                    case IConvertible convertible:
                        result = convertible.ToInt16(NumberFormatInfo.InvariantInfo);
                        break;
                }
                return !allowNegative && result < 0 ? defaultValue : result;
            }
            catch (FormatException)
            {
                return defaultValue;
            }
            catch (OverflowException)
            {
                return defaultValue;
            }
        }

19 Source : Convert.cs
with Apache License 2.0
from theolivenbaum

internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
        {
            Debug.replacedert(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (ReferenceEquals(value.GetType(), targetType))
            {
                return value;
            }

            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Boolean]))
                return value.ToBoolean(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Char]))
                return value.ToChar(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.SByte]))
                return value.ToSByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Byte]))
                return value.ToByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int16]))
                return value.ToInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt16]))
                return value.ToUInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int32]))
                return value.ToInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt32]))
                return value.ToUInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int64]))
                return value.ToInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt64]))
                return value.ToUInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Single]))
                return value.ToSingle(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Double]))
                return value.ToDouble(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Decimal]))
                return value.ToDecimal(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DateTime]))
                return value.ToDateTime(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.String]))
                return value.ToString(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Object]))
                return (object)value;
            //  Need to special case Enum because typecode will be underlying type, e.g. Int32
            if (ReferenceEquals(targetType, EnumType))
                return (Enum)value;
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DBNull]))
                throw new InvalidCastException("Object cannot be cast to DBNull.");
            // TODO: SR
            //throw new InvalidCastException(SR.InvalidCast_DBNull);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Empty]))
                throw new InvalidCastException("Object cannot be cast to Empty.");
            // TODO: SR
            //throw new InvalidCastException(SR.InvalidCast_Empty);

            throw new InvalidCastException(string.Format("Invalid cast from '{0}' to '{1}'.", value.GetType().FullName, targetType.FullName));
            // TODO: SR
            //throw new InvalidCastException(string.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName));
        }

19 Source : NumberSerializer.cs
with Apache License 2.0
from tonyredondo

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Write(BinaryWriter writer, object value)
        {
            var valueType = value?.GetType();
            var decType = DataTypeHelper.GetDecreaseDataType(value, valueType);
            var convValue = (IConvertible) value ?? 0;
            int objIdx;
            switch (decType)
            {
                case DataType.Decimal:
                    #region Decimal Type
                    var v1 = convValue.ToDecimal(null);
                    if (v1 == default(decimal))
                    {
                        writer.Write(DataType.DecimalDefault);
                        return;
                    }
                    objIdx = DecimalCache.SerializerGet(v1);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefDecimalByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefDecimalUShort, (ushort)objIdx);
                    }
                    else
                    {
                        writer.Write(DataType.Decimal);
                        writer.Write(v1);
                        DecimalCache.SerializerSet(v1);
                    }
                    #endregion
                    return;
                case DataType.Double:
                    #region Double Type
                    var v2 = convValue.ToDouble(null);
                    if (Math.Abs(v2 - default(double)) < 0.0000000000001)
                    {
                        writer.Write(DataType.DoubleDefault);
                        return;
                    }
                    objIdx = DoubleCache.SerializerGet(v2);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefDoubleByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefDoubleUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteDouble(writer, DataType.Double, v2);
                        DoubleCache.SerializerSet(v2);
                    }
                    #endregion
                    return;
                case DataType.Float:
                    #region Float Type
                    var v3 = convValue.ToSingle(null);
                    if (Math.Abs(v3 - default(float)) < 0.0000000000001)
                    {
                        writer.Write(DataType.FloatDefault);
                        return;
                    }
                    objIdx = FloatCache.SerializerGet(v3);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefFloatByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefFloatUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteFloat(writer, DataType.Float, v3);
                        FloatCache.SerializerSet(v3);
                    }
                    #endregion
                    return;
                case DataType.Long:
                    #region Long Type
                    var v4 = convValue.ToInt64(null);
                    objIdx = LongCache.SerializerGet(v4);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefLongByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefLongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteLong(writer, DataType.Long, v4);
                        LongCache.SerializerSet(v4);
                    }
                    #endregion
                    return;
                case DataType.ULong:
                    #region ULong Type
                    var v5 = convValue.ToUInt64(null);
                    objIdx = ULongCache.SerializerGet(v5);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefULongByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefULongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteULong(writer, DataType.ULong, v5);
                        ULongCache.SerializerSet(v5);
                    }
                    #endregion
                    return;
                case DataType.Int:
                    #region Int Type
                    var v6 = convValue.ToInt32(null);
                    objIdx = IntCache.SerializerGet(v6);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteInt(writer, DataType.Int, v6);
                        IntCache.SerializerSet(v6);
                    }
                    #endregion
                    return;
                case DataType.UInt:
                    #region UInt Type
                    var v7 = convValue.ToUInt32(null);
                    objIdx = UIntCache.SerializerGet(v7);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefUIntByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefUIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteUInt(writer, DataType.UInt, v7);
                        UIntCache.SerializerSet(v7);
                    }
                    #endregion
                    return;
                case DataType.Short:
                    #region Short Type
                    var v8 = convValue.ToInt16(null);
                    objIdx = ShortCache.SerializerGet(v8);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteHelper.WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                    else
                    {
                        WriteHelper.WriteShort(writer, DataType.Short, v8);
                        ShortCache.SerializerSet(v8);
                    }
                    #endregion
                    return;
                case DataType.UShort:
                    #region UShort Type
                    var v9 = convValue.ToUInt16(null);
                    objIdx = UShortCache.SerializerGet(v9);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteHelper.WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                    else
                    {
                        WriteHelper.WriteUshort(writer, DataType.UShort, v9);
                        UShortCache.SerializerSet(v9);
                    }
                    #endregion
                    return;
                case DataType.Byte:
                    #region Byte Type
                    var v10 = convValue.ToByte(null);
                    switch (v10)
                    {
                        case 0:
                            writer.Write(DataType.ByteDefault);
                            return;
                        case 1:
                            writer.Write(DataType.Byte1);
                            return;
                        case 2:
                            writer.Write(DataType.Byte2);
                            return;
                        case 3:
                            writer.Write(DataType.Byte3);
                            return;
                        case 4:
                            writer.Write(DataType.Byte4);
                            return;
                        case 5:
                            writer.Write(DataType.Byte5);
                            return;
                        case 6:
                            writer.Write(DataType.Byte6);
                            return;
                        case 7:
                            writer.Write(DataType.Byte7);
                            return;
                        case 8:
                            writer.Write(DataType.Byte8);
                            return;
                        case 9:
                            writer.Write(DataType.Byte9);
                            return;
                        case 10:
                            writer.Write(DataType.Byte10);
                            return;
                        case 11:
                            writer.Write(DataType.Byte11);
                            return;
                        case 12:
                            writer.Write(DataType.Byte12);
                            return;
                        case 13:
                            writer.Write(DataType.Byte13);
                            return;
                        case 14:
                            writer.Write(DataType.Byte14);
                            return;
                        case 15:
                            writer.Write(DataType.Byte15);
                            return;
                        case 16:
                            writer.Write(DataType.Byte16);
                            return;
                        case 17:
                            writer.Write(DataType.Byte17);
                            return;
                        case 18:
                            writer.Write(DataType.Byte18);
                            return;
                        case 19:
                            writer.Write(DataType.Byte19);
                            return;
                        case 20:
                            writer.Write(DataType.Byte20);
                            return;
                        default:
                            WriteHelper.WriteByte(writer, DataType.Byte, v10);
                            return;
                    }
                #endregion
                case DataType.SByte:
                    #region SByte Type
                    var sByte = convValue.ToSByte(null);
                    switch (sByte)
                    {
                        case -1:
                            writer.Write(DataType.SByteMinusOne);
                            return;
                        default:
                            writer.Write(DataType.SByte);
                            writer.Write(sByte);
                            return;
                    }
                    #endregion
            }
        }

19 Source : NumberSerializer.cs
with Apache License 2.0
from tonyredondo

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override void Write(BinaryWriter writer, object value)
        {
            var valueType = value?.GetType();
            var decType = DataTypeHelper.GetDecreaseDataType(value, valueType);
            var convValue = (IConvertible) value ?? 0;
            int objIdx;
            switch (decType)
            {
                case DataType.Decimal:
                    #region Decimal Type
                    var v1 = convValue.ToDecimal(null);
                    if (v1 == default(decimal))
                    {
                        writer.Write(DataType.DecimalDefault);
                        return;
                    }
                    objIdx = DecimalCache.SerializerGet(v1);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefDecimalByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefDecimalUShort, (ushort)objIdx);
                    }
                    else
                    {
                        writer.Write(DataType.Decimal);
                        writer.Write(v1);
                        DecimalCache.SerializerSet(v1);
                    }
                    #endregion
                    return;
                case DataType.Double:
                    #region Double Type
                    var v2 = convValue.ToDouble(null);
                    if (Math.Abs(v2 - default(double)) < 0.0000000000001)
                    {
                        writer.Write(DataType.DoubleDefault);
                        return;
                    }
                    objIdx = DoubleCache.SerializerGet(v2);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefDoubleByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefDoubleUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteDouble(writer, DataType.Double, v2);
                        DoubleCache.SerializerSet(v2);
                    }
                    #endregion
                    return;
                case DataType.Float:
                    #region Float Type
                    var v3 = convValue.ToSingle(null);
                    if (Math.Abs(v3 - default(float)) < 0.0000000000001)
                    {
                        writer.Write(DataType.FloatDefault);
                        return;
                    }
                    objIdx = FloatCache.SerializerGet(v3);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefFloatByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefFloatUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteFloat(writer, DataType.Float, v3);
                        FloatCache.SerializerSet(v3);
                    }
                    #endregion
                    return;
                case DataType.Long:
                    #region Long Type
                    var v4 = convValue.ToInt64(null);
                    objIdx = LongCache.SerializerGet(v4);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefLongByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefLongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteLong(writer, DataType.Long, v4);
                        LongCache.SerializerSet(v4);
                    }
                    #endregion
                    return;
                case DataType.ULong:
                    #region ULong Type
                    var v5 = convValue.ToUInt64(null);
                    objIdx = ULongCache.SerializerGet(v5);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefULongByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefULongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteULong(writer, DataType.ULong, v5);
                        ULongCache.SerializerSet(v5);
                    }
                    #endregion
                    return;
                case DataType.Int:
                    #region Int Type
                    var v6 = convValue.ToInt32(null);
                    objIdx = IntCache.SerializerGet(v6);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteInt(writer, DataType.Int, v6);
                        IntCache.SerializerSet(v6);
                    }
                    #endregion
                    return;
                case DataType.UInt:
                    #region UInt Type
                    var v7 = convValue.ToUInt32(null);
                    objIdx = UIntCache.SerializerGet(v7);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefUIntByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefUIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteUInt(writer, DataType.UInt, v7);
                        UIntCache.SerializerSet(v7);
                    }
                    #endregion
                    return;
                case DataType.Short:
                    #region Short Type
                    var v8 = convValue.ToInt16(null);
                    objIdx = ShortCache.SerializerGet(v8);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                    else
                    {
                        WriteShort(writer, DataType.Short, v8);
                        ShortCache.SerializerSet(v8);
                    }
                    #endregion
                    return;
                case DataType.UShort:
                    #region UShort Type
                    var v9 = convValue.ToUInt16(null);
                    objIdx = UShortCache.SerializerGet(v9);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                    else
                    {
                        WriteUshort(writer, DataType.UShort, v9);
                        UShortCache.SerializerSet(v9);
                    }
                    #endregion
                    return;
                case DataType.Byte:
                    #region Byte Type
                    var v10 = convValue.ToByte(null);
                    switch (v10)
                    {
                        case 0:
                            writer.Write(DataType.ByteDefault);
                            return;
                        case 1:
                            writer.Write(DataType.Byte1);
                            return;
                        case 2:
                            writer.Write(DataType.Byte2);
                            return;
                        case 3:
                            writer.Write(DataType.Byte3);
                            return;
                        case 4:
                            writer.Write(DataType.Byte4);
                            return;
                        case 5:
                            writer.Write(DataType.Byte5);
                            return;
                        case 6:
                            writer.Write(DataType.Byte6);
                            return;
                        case 7:
                            writer.Write(DataType.Byte7);
                            return;
                        case 8:
                            writer.Write(DataType.Byte8);
                            return;
                        case 9:
                            writer.Write(DataType.Byte9);
                            return;
                        case 10:
                            writer.Write(DataType.Byte10);
                            return;
                        case 11:
                            writer.Write(DataType.Byte11);
                            return;
                        case 12:
                            writer.Write(DataType.Byte12);
                            return;
                        case 13:
                            writer.Write(DataType.Byte13);
                            return;
                        case 14:
                            writer.Write(DataType.Byte14);
                            return;
                        case 15:
                            writer.Write(DataType.Byte15);
                            return;
                        case 16:
                            writer.Write(DataType.Byte16);
                            return;
                        default:
                            WriteByte(writer, DataType.Byte, v10);
                            return;
                    }
                #endregion
                case DataType.SByte:
                    #region SByte Type
                    var sByte = convValue.ToSByte(null);
                    switch (sByte)
                    {
                        case -1:
                            writer.Write(DataType.SByteMinusOne);
                            return;
                        default:
                            writer.Write(DataType.SByte);
                            writer.Write(sByte);
                            return;
                    }
                    #endregion
            }
        }

19 Source : Int7V.cs
with GNU General Public License v3.0
from tyranid

short IConvertible.ToInt16(IFormatProvider provider)
        {
            IConvertible conv = (IConvertible)value;

            return conv.ToInt16(provider);
        }

19 Source : NIntNUInt.cs
with MIT License
from xamarin

short IConvertible.ToInt16 (IFormatProvider provider) { return ((IConvertible)v).ToInt16 (provider); }

19 Source : KalkBool.cs
with BSD 2-Clause "Simplified" License
from xoofx

short IConvertible.ToInt16(IFormatProvider? provider)
        {
            return ((IConvertible) _value).ToInt16(provider);
        }

19 Source : KTIDReference.cs
with MIT License
from yretenai

public short ToInt16(IFormatProvider? provider) => ((IConvertible) KTID).ToInt16(provider);