GetTypeCode() public static method

public static GetTypeCode ( object value ) : TypeCode
value object
return TypeCode
Example #1
0
        private static String InternalFormattedHexString(object value)
        {
            TypeCode typeCode = Convert.GetTypeCode(value);

            switch (typeCode)
            {
            case TypeCode.SByte:
                return(((byte)(sbyte)value).ToString("X2", null));

            case TypeCode.Byte:
                return(((byte)value).ToString("X2", null));

            case TypeCode.Boolean:
                // direct cast from bool to byte is not allowed
                return(Convert.ToByte((bool)value).ToString("X2", null));

            case TypeCode.Int16:
                return(((UInt16)(Int16)value).ToString("X4", null));

            case TypeCode.UInt16:
                return(((UInt16)value).ToString("X4", null));

            case TypeCode.Char:
                return(((UInt16)(Char)value).ToString("X4", null));

            case TypeCode.UInt32:
                return(((UInt32)value).ToString("X8", null));

            case TypeCode.Int32:
                return(((UInt32)(Int32)value).ToString("X8", null));

            case TypeCode.UInt64:
                return(((UInt64)value).ToString("X16", null));

            case TypeCode.Int64:
                return(((UInt64)(Int64)value).ToString("X16", null));

            // All unsigned types will be directly cast
            default:
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
            }
        }
Example #2
0
File: Enum.cs Project: relaxar/.net
        private static string InternalFormattedHexString(object value)
        {
            TypeCode typeCode = Convert.GetTypeCode(value);

            switch (typeCode)
            {
            case TypeCode.SByte:
                return(((byte)(sbyte)value).ToString("X2", null));

            case TypeCode.Byte:
                return(((byte)value).ToString("X2", null));

            case TypeCode.Boolean:
                // direct cast from bool to byte is not allowed
                return(Convert.ToByte((bool)value).ToString("X2", null));

            case TypeCode.Int16:
                return(((ushort)(short)value).ToString("X4", null));

            case TypeCode.UInt16:
                return(((ushort)value).ToString("X4", null));

            case TypeCode.Char:
                return(((ushort)(char)value).ToString("X4", null));

            case TypeCode.UInt32:
                return(((uint)value).ToString("X8", null));

            case TypeCode.Int32:
                return(((uint)(int)value).ToString("X8", null));

            case TypeCode.UInt64:
                return(((ulong)value).ToString("X16", null));

            case TypeCode.Int64:
                return(((ulong)(long)value).ToString("X16", null));

            // All unsigned types will be directly cast
            default:
                throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType);
            }
        }
Example #3
0
 /// <summary>
 /// Check is a Type is a typical "primitive" Type
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsSimpleType(this Type type)
 {
     return
         (type.IsPrimitive ||
          new Type[] {
         typeof(Enum),
         typeof(String),
         typeof(Decimal),
         typeof(DateTime),
         typeof(DateTimeOffset),
         typeof(TimeSpan),
         typeof(Guid)
     }.Contains(type) ||
          Convert.GetTypeCode(type) != TypeCode.Object ||
          (
              type.IsGenericType &&
              type.GetGenericTypeDefinition() == typeof(Nullable <>) &&
              IsSimpleType(type.GetGenericArguments()[0])
          ));
 }
Example #4
0
        public static Object ToObject(Type enumType, Object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // Delegate rest of error checking to the other functions
            TypeCode typeCode = Convert.GetTypeCode(value);

            switch (typeCode)
            {
            case TypeCode.Int32:
                return(ToObject(enumType, (int)value));

            case TypeCode.SByte:
                return(ToObject(enumType, (sbyte)value));

            case TypeCode.Int16:
                return(ToObject(enumType, (short)value));

            case TypeCode.Int64:
                return(ToObject(enumType, (long)value));

            case TypeCode.UInt32:
                return(ToObject(enumType, (uint)value));

            case TypeCode.Byte:
                return(ToObject(enumType, (byte)value));

            case TypeCode.UInt16:
                return(ToObject(enumType, (ushort)value));

            case TypeCode.UInt64:
                return(ToObject(enumType, (ulong)value));

            default:
                // All unsigned types will be directly cast
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");
            }
        }
Example #5
0
        /// <summary>
        /// Determines whether or not the type is a system type.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <returns>[true] if is a system type, otherwise [false].</returns>
        public static bool IsSystemType(this Type type)
        {
            if (type == null)
            {
                return(false);
            }

            return
                (type.IsPrimitive ||
                 new [] {
                typeof(string),
                typeof(decimal),
                typeof(DateTime),
                typeof(DateTimeOffset),
                typeof(TimeSpan),
                typeof(Guid)
            }.Contains(type) ||
                 type.IsEnum ||
                 Convert.GetTypeCode(type) != TypeCode.Object ||
                 (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>) && IsSystemType(type.GetGenericArguments().FirstOrDefault())));
        }
Example #6
0
        public static ulong ToUInt64(object value)
        {
            switch (Convert.GetTypeCode(value))
            {
            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                return((UInt64)Convert.ToInt64(value, CultureInfo.InvariantCulture));

            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Boolean:
            case TypeCode.Char:
                return(Convert.ToUInt64(value, CultureInfo.InvariantCulture));

            default:
                throw new ArgumentException("Value is not the enum or a valid enum underlying type", "value");
            }
        }
Example #7
0
        private static String InternalFormattedHexString(Object value)
        {
            TypeCode typeCode = Convert.GetTypeCode(value);

            switch (typeCode)
            {
            case TypeCode.SByte:
            {
                Byte result = (byte)(sbyte)value;

                return(result.ToString("X2", null));
            }

            case TypeCode.Byte:
            {
                Byte result = (byte)value;

                return(result.ToString("X2", null));
            }

            case TypeCode.Boolean:
            {
                // direct cast from bool to byte is not allowed
                Byte result = Convert.ToByte((bool)value);

                return(result.ToString("X2", null));
            }

            case TypeCode.Int16:
            {
                UInt16 result = (UInt16)(Int16)value;

                return(result.ToString("X4", null));
            }

            case TypeCode.UInt16:
            {
                UInt16 result = (UInt16)value;

                return(result.ToString("X4", null));
            }

            case TypeCode.Char:
            {
                UInt16 result = (UInt16)(Char)value;

                return(result.ToString("X4", null));
            }

            case TypeCode.UInt32:
            {
                UInt32 result = (UInt32)value;

                return(result.ToString("X8", null));
            }

            case TypeCode.Int32:
            {
                UInt32 result = (UInt32)(int)value;

                return(result.ToString("X8", null));
            }

            case TypeCode.UInt64:
            {
                UInt64 result = (UInt64)value;

                return(result.ToString("X16", null));
            }

            case TypeCode.Int64:
            {
                UInt64 result = (UInt64)(Int64)value;

                return(result.ToString("X16", null));
            }

            // All unsigned types will be directly cast
            default:
                Contract.Assert(false, "Invalid Object type in Format");
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
            }
        }
Example #8
0
 public TypeCode GetTypeCode()
 {
     return(Convert.GetTypeCode(_data.Reverse()));
 }
Example #9
0
        private static string InternalFormattedHexString(object value)
        {
            switch (Convert.GetTypeCode(value))
            {
            case TypeCode.SByte:
            {
                sbyte n = (sbyte)value;
                return(((byte)n).ToString("X2", null));
            }

            case TypeCode.Byte:
            {
                byte n = (byte)value;
                return(n.ToString("X2", null));
            }

            case TypeCode.Boolean:
            {
                bool n = (bool)value;
                return(Convert.ToByte(n).ToString("X2", null));
            }

            case TypeCode.Int16:
            {
                short n = (short)value;
                return(((ushort)n).ToString("X4", null));
            }

            case TypeCode.UInt16:
            {
                ushort n = (ushort)value;
                return(n.ToString("X4", null));
            }

            case TypeCode.Char:
            {
                char n = (char)value;
                return(((ushort)n).ToString("X4", null));
            }

            case TypeCode.Int32:
            {
                int n = (int)value;
                return(((uint)n).ToString("X8", null));
            }

            case TypeCode.UInt32:
            {
                uint n = (uint)value;
                return(n.ToString("X8", null));
            }

            case TypeCode.Int64:
            {
                int n = (int)value;
                return(((ulong)n).ToString("X16", null));
            }

            case TypeCode.UInt64:
            {
                uint n = (uint)value;
                return(n.ToString("X16", null));
            }
            }

            throw new InvalidOperationException("Unknown enum type");
        }
Example #10
0
 public TypeCode GetTypeCode() => Convert.GetTypeCode(_stringValue);
Example #11
0
        // ReSharper restore StaticMemberInGenericType

        static EnumHelper()
        {
            var enumType = typeof(EnumT);

#if NETSTANDARD
            var enumTypeInfo = enumType.GetTypeInfo();
#else
            var enumTypeInfo = enumType;
#endif
            if (enumTypeInfo.IsEnum == false)
            {
                throw new InvalidOperationException("EnumT should be enum type.");
            }

            var underlyingType = Enum.GetUnderlyingType(enumType);
            var valueParameter = Expression.Parameter(underlyingType, "value");
            var enumParameter  = Expression.Parameter(enumType, "value");
            var xParameter     = Expression.Parameter(enumType, "value");
            var yParameter     = Expression.Parameter(enumType, "value");
            var instance       = Activator.CreateInstance(enumType);

            UnderlyingType = underlyingType;
            TypeCode       = Convert.GetTypeCode(instance);
            IsFlags        = enumTypeInfo.GetCustomAttributes(typeof(FlagsAttribute), false).Any();
            IsSigned       = TypeCode == TypeCode.SByte || TypeCode == TypeCode.Int16 || TypeCode == TypeCode.Int32 || TypeCode == TypeCode.Int64;
            FromNumber     = Expression.Lambda(Expression.ConvertChecked(valueParameter, enumType), valueParameter).Compile();
            ToNumber       = Expression.Lambda(Expression.ConvertChecked(enumParameter, underlyingType), enumParameter).Compile();
            Comparer       = new ComparisonComparer <EnumT>(Expression.Lambda <Comparison <EnumT> >(
                                                                Expression.Call
                                                                (
                                                                    Expression.ConvertChecked(xParameter, underlyingType),
                                                                    "CompareTo",
                                                                    Type.EmptyTypes,
                                                                    Expression.ConvertChecked(yParameter, underlyingType)
                                                                ),
                                                                xParameter,
                                                                yParameter
                                                                ).Compile());

            NamesByValue = new SortedDictionary <EnumT, string>(Comparer);
            ValueByName  = new SortedDictionary <string, EnumT>(StringComparer.Ordinal);
            DefaultValue = default(EnumT);

            var valuesArray = Enum.GetValues(enumType);
            var names       = new List <string>(valuesArray.Length);
            var values      = new List <EnumT>(valuesArray.Length);
            foreach (EnumT value in valuesArray)
            {
                var name = Enum.GetName(enumType, value);
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                NamesByValue[value] = name;
                ValueByName[name]   = value;
                names.Add(name);
                values.Add(value);

                MinValue = Comparer.Compare(value, MinValue) < 0 ? value : MinValue;
                MaxValue = Comparer.Compare(value, MaxValue) > 0 ? value : MaxValue;
            }

            if (values.Contains(DefaultValue) == false)
            {
                // if default value is not part of Values then
                // swap MinValue and MaxValue and re-calculate them
                var tempMaxValue = MaxValue;
                MaxValue = MinValue;
                MinValue = tempMaxValue;

                foreach (var value in values)
                {
                    MinValue = Comparer.Compare(value, MinValue) < 0 ? value : MinValue;
                    MaxValue = Comparer.Compare(value, MaxValue) > 0 ? value : MaxValue;
                }
            }

            Values = values.AsReadOnly();
            Names  = names.AsReadOnly();
        }