Esempio n. 1
0
        //
        // Note: This works on both Enum's and underlying integer values.
        //
        //
        // This returns the underlying enum values as "ulong" regardless of the actual underlying type. Signed integral
        // types get sign-extended into the 64-bit value, unsigned types get zero-extended.
        //
        // The return value is "bool" if "value" is not an enum or an "integer type" as defined by the BCL Enum apis.
        //
        internal static bool TryGetUnboxedValueOfEnumOrInteger(object value, out ulong result)
        {
            EETypePtr eeType = value.GetEETypePtr();

            // For now, this check is required to flush out pointers.
            if (!eeType.IsDefType)
            {
                result = 0;
                return(false);
            }
            EETypeElementType elementType = eeType.ElementType;

            ref byte pValue = ref value.GetRawData();
Esempio n. 2
0
        public static TypeCode GetRuntimeTypeCode(Type type)
        {
            Debug.Assert(type != null);

            EETypePtr eeType;

            if (!type.TryGetEEType(out eeType))
            {
                // Type exists in metadata only. Aside from the enums, there is no chance a type with a TypeCode would not have an MethodTable,
                // so if it's not an enum, return the default.
                if (!type.IsEnum)
                {
                    return(TypeCode.Object);
                }
                Type underlyingType = Enum.GetUnderlyingType(type);
                eeType = underlyingType.TypeHandle.ToEETypePtr();
            }

            // Note: Type.GetTypeCode() is expected to return the underlying type's TypeCode for enums. EETypePtr.CorElementType does the same,
            // so this one switch handles both cases.
            EETypeElementType rhType = eeType.ElementType;

            switch (rhType)
            {
            case EETypeElementType.Boolean: return(TypeCode.Boolean);

            case EETypeElementType.Char: return(TypeCode.Char);

            case EETypeElementType.SByte: return(TypeCode.SByte);

            case EETypeElementType.Byte: return(TypeCode.Byte);

            case EETypeElementType.Int16: return(TypeCode.Int16);

            case EETypeElementType.UInt16: return(TypeCode.UInt16);

            case EETypeElementType.Int32: return(TypeCode.Int32);

            case EETypeElementType.UInt32: return(TypeCode.UInt32);

            case EETypeElementType.Int64: return(TypeCode.Int64);

            case EETypeElementType.UInt64: return(TypeCode.UInt64);

            case EETypeElementType.Single: return(TypeCode.Single);

            case EETypeElementType.Double: return(TypeCode.Double);

            default:
                break;
            }

            if (type == typeof(string))
            {
                return(TypeCode.String);
            }

            if (type == typeof(DateTime))
            {
                return(TypeCode.DateTime);
            }

            if (type == typeof(decimal))
            {
                return(TypeCode.Decimal);
            }

            if (type == typeof(DBNull))
            {
                return(TypeCode.DBNull);
            }

            return(TypeCode.Object);
        }