GetUnderlyingType() private method

private GetUnderlyingType ( Type enumType ) : Type
enumType Type
return Type
        public static List <IEnumValue> GetEnumValues(Type type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.IsEnum)
            {
                List <IEnumValue> Values = new List <IEnumValue>();

                foreach (string Name in Enum.GetNames(type))
                {
                    Values.Add(
                        new EnumValue()
                    {
                        Value = Convert.ChangeType(
                            Enum.Parse(type, Name),
                            Enum.GetUnderlyingType(type)
                            ).ToString(),
                        Label = Name
                    });
                }

                return(Values);
            }
            else
            {
                return(null);
            }
        }
        static void GenEnum(Type type, TypeStatus ts,
                            Func <Type, TypeStatus> getParent, Action <Type> onNewType)
        {
            TextFile tfFile = null;

            if (type.DeclaringType != null)
            {
                onNewType(type.DeclaringType);
                ts.IsInnerType = true;

                TypeStatus tsParent = getParent(type.DeclaringType);
                if (tsParent == null || tsParent.status == TypeStatus.Status.Wait)
                {
                    if (tsParent == null)
                    {
                        onNewType(type.DeclaringType);
                    }
                    return;
                }

                if (tsParent.status == TypeStatus.Status.Ignored)
                {
                    ts.status = TypeStatus.Status.Ignored;
                    return;
                }

                tfFile = tsParent.tf.FindByTag("epos");
            }

            if (tfFile == null)
            {
                tfFile = new TextFile();
            }

            ts.tf     = tfFile;
            ts.status = TypeStatus.Status.Exported;

            //GeneratorHelp.ATypeInfo ti = GeneratorHelp.CreateTypeInfo(type);

            StringBuilder sb   = new StringBuilder();
            TextFile      tfNs = tfFile;

            if (type.DeclaringType == null &&
                !string.IsNullOrEmpty(type.Namespace))
            {
                tfNs = tfFile.Add("namespace {0}", type.Namespace).BraceIn();
                tfNs.BraceOut();
            }

            GenAttributeForClassIfNeeded(type, tfNs);

            Type     uType   = Enum.GetUnderlyingType(type);
            TextFile tfClass = null;

            sb.Remove(0, sb.Length);
            {
                // if (type.IsPublic || type.IsNestedPublic)
                //     sb.Append("public ");

                sb.Append("public enum ");
                sb.Append(type.Name);
                if (uType != typeof(int))
                {
                    sb.AppendFormat(" : {0}", uType.CsFullName());
                }

                tfClass = tfNs.Add(sb.ToString()).BraceIn();
                tfClass.BraceOut();
            }

            FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];
                string    v     = "";
                if (uType == typeof(ulong))
                {
                    v = System.Convert.ToUInt64(field.GetValue(null)).ToString();
                }
                else
                {
                    v = System.Convert.ToInt64(field.GetValue(null)).ToString();
                }

                tfClass.Add("{0} = {1},", field.Name, v);
            }
        }
示例#3
0
        // Binder uses some incompatible conversion rules. For example
        // int value cannot be used with decimal parameter but in other
        // ways it's more flexible than normal convertor, for example
        // long value can be used with int based enum
        static object IsConvertibleToPrimitiveType(object value, Type targetType)
        {
            var type = value.GetType();

            if (type.IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
                if (type == targetType)
                {
                    return(value);
                }
            }

            var from = Type.GetTypeCode(type);
            var to   = Type.GetTypeCode(targetType);

            switch (to)
            {
            case TypeCode.Char:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Char)(Byte)value);

                case TypeCode.UInt16:
                    return(value);
                }
                break;

            case TypeCode.Int16:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Int16)(Byte)value);

                case TypeCode.SByte:
                    return((Int16)(SByte)value);
                }
                break;

            case TypeCode.UInt16:
                switch (from)
                {
                case TypeCode.Byte:
                    return((UInt16)(Byte)value);

                case TypeCode.Char:
                    return(value);
                }
                break;

            case TypeCode.Int32:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Int32)(Byte)value);

                case TypeCode.SByte:
                    return((Int32)(SByte)value);

                case TypeCode.Char:
                    return((Int32)(Char)value);

                case TypeCode.Int16:
                    return((Int32)(Int16)value);

                case TypeCode.UInt16:
                    return((Int32)(UInt16)value);
                }
                break;

            case TypeCode.UInt32:
                switch (from)
                {
                case TypeCode.Byte:
                    return((UInt32)(Byte)value);

                case TypeCode.Char:
                    return((UInt32)(Char)value);

                case TypeCode.UInt16:
                    return((UInt32)(UInt16)value);
                }
                break;

            case TypeCode.Int64:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Int64)(Byte)value);

                case TypeCode.SByte:
                    return((Int64)(SByte)value);

                case TypeCode.Int16:
                    return((Int64)(Int16)value);

                case TypeCode.Char:
                    return((Int64)(Char)value);

                case TypeCode.UInt16:
                    return((Int64)(UInt16)value);

                case TypeCode.Int32:
                    return((Int64)(Int32)value);

                case TypeCode.UInt32:
                    return((Int64)(UInt32)value);
                }
                break;

            case TypeCode.UInt64:
                switch (from)
                {
                case TypeCode.Byte:
                    return((UInt64)(Byte)value);

                case TypeCode.Char:
                    return((UInt64)(Char)value);

                case TypeCode.UInt16:
                    return((UInt64)(UInt16)value);

                case TypeCode.UInt32:
                    return((UInt64)(UInt32)value);
                }
                break;

            case TypeCode.Single:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Single)(Byte)value);

                case TypeCode.SByte:
                    return((Single)(SByte)value);

                case TypeCode.Int16:
                    return((Single)(Int16)value);

                case TypeCode.Char:
                    return((Single)(Char)value);

                case TypeCode.UInt16:
                    return((Single)(UInt16)value);

                case TypeCode.Int32:
                    return((Single)(Int32)value);

                case TypeCode.UInt32:
                    return((Single)(UInt32)value);

                case TypeCode.Int64:
                    return((Single)(Int64)value);

                case TypeCode.UInt64:
                    return((Single)(UInt64)value);
                }
                break;

            case TypeCode.Double:
                switch (from)
                {
                case TypeCode.Byte:
                    return((Double)(Byte)value);

                case TypeCode.SByte:
                    return((Double)(SByte)value);

                case TypeCode.Char:
                    return((Double)(Char)value);

                case TypeCode.Int16:
                    return((Double)(Int16)value);

                case TypeCode.UInt16:
                    return((Double)(UInt16)value);

                case TypeCode.Int32:
                    return((Double)(Int32)value);

                case TypeCode.UInt32:
                    return((Double)(UInt32)value);

                case TypeCode.Int64:
                    return((Double)(Int64)value);

                case TypeCode.UInt64:
                    return((Double)(UInt64)value);

                case TypeCode.Single:
                    return((Double)(Single)value);
                }
                break;
            }

            // Everything else is rejected
            return(null);
        }
示例#4
0
        protected override TypeCode GetTypeCodeImpl()
        {
            TypeCode typeCode = Cache.TypeCode;

            if (typeCode != TypeCode.Empty)
            {
                return(typeCode);
            }

            CorElementType corElementType = RuntimeTypeHandle.GetCorElementType(this);

            switch (corElementType)
            {
            case CorElementType.ELEMENT_TYPE_BOOLEAN:
                typeCode = TypeCode.Boolean; break;

            case CorElementType.ELEMENT_TYPE_CHAR:
                typeCode = TypeCode.Char; break;

            case CorElementType.ELEMENT_TYPE_I1:
                typeCode = TypeCode.SByte; break;

            case CorElementType.ELEMENT_TYPE_U1:
                typeCode = TypeCode.Byte; break;

            case CorElementType.ELEMENT_TYPE_I2:
                typeCode = TypeCode.Int16; break;

            case CorElementType.ELEMENT_TYPE_U2:
                typeCode = TypeCode.UInt16; break;

            case CorElementType.ELEMENT_TYPE_I4:
                typeCode = TypeCode.Int32; break;

            case CorElementType.ELEMENT_TYPE_U4:
                typeCode = TypeCode.UInt32; break;

            case CorElementType.ELEMENT_TYPE_I8:
                typeCode = TypeCode.Int64; break;

            case CorElementType.ELEMENT_TYPE_U8:
                typeCode = TypeCode.UInt64; break;

            case CorElementType.ELEMENT_TYPE_R4:
                typeCode = TypeCode.Single; break;

            case CorElementType.ELEMENT_TYPE_R8:
                typeCode = TypeCode.Double; break;

            case CorElementType.ELEMENT_TYPE_VALUETYPE:
                if (this == Convert.ConvertTypes[(int)TypeCode.Decimal])
                {
                    typeCode = TypeCode.Decimal;
                }
                else if (this == Convert.ConvertTypes[(int)TypeCode.DateTime])
                {
                    typeCode = TypeCode.DateTime;
                }
                else if (IsEnum)
                {
                    typeCode = GetTypeCode(Enum.GetUnderlyingType(this));
                }
                else
                {
                    typeCode = TypeCode.Object;
                }
                break;

            default:
                if (this == Convert.ConvertTypes[(int)TypeCode.DBNull])
                {
                    typeCode = TypeCode.DBNull;
                }
                else if (this == Convert.ConvertTypes[(int)TypeCode.String])
                {
                    typeCode = TypeCode.String;
                }
                else
                {
                    typeCode = TypeCode.Object;
                }
                break;
            }

            Cache.TypeCode = typeCode;

            return(typeCode);
        }
示例#5
0
        public static string Format(Type enumType, object value, string format)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("enumType is not an Enum type.", "enumType");
            }

            Type vType          = value.GetType();
            Type underlyingType = Enum.GetUnderlyingType(enumType);

            if (vType.IsEnum)
            {
                if (vType != enumType)
                {
                    throw new ArgumentException(string.Format(
                                                    "Object must be the same type as the enum. The type" +
                                                    " passed in was {0}; the enum type was {1}.",
                                                    vType.FullName, enumType.FullName));
                }
            }
            else if (vType != underlyingType)
            {
                throw new ArgumentException(string.Format(
                                                "Enum underlying type and the object must be the same type" +
                                                " or object. Type passed in was {0}; the enum underlying" +
                                                " type was {1}.", vType.FullName, underlyingType.FullName));
            }

            if (format.Length == 1)
            {
                switch (format [0])
                {
                case 'f':
                case 'F':
                    return(FormatFlags(enumType, value));

                case 'g':
                case 'G':
                    if (!enumType.IsDefined(typeof(FlagsAttribute), false))
                    {
                        return(GetName(enumType, value) ?? value.ToString());
                    }

                    goto case 'f';

                case 'X':
                    return(FormatSpecifier_X(enumType, value, true));

                case 'x':
                    return(FormatSpecifier_X(enumType, value, false));

                case 'D':
                case 'd':
                    if (vType.IsEnum)
                    {
                        value = ((Enum)value).Value;
                    }

                    return(value.ToString());
                }
            }

            throw new FormatException("Format String can be only \"G\",\"g\",\"X\"," +
                                      "\"x\",\"F\",\"f\",\"D\" or \"d\".");
        }
示例#6
0
 public static Type UnwrapEnumType(this Type type)
 => type.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(type) : type;
示例#7
0
		public static string Format (Type enumType, object value, string format)
		{
			if (enumType == null)
				throw new ArgumentNullException ("enumType");
			if (value == null)
				throw new ArgumentNullException ("value");
			if (format == null)
				throw new ArgumentNullException ("format");

			if (!enumType.IsEnum)
				throw new ArgumentException ("enumType is not an Enum type.", "enumType");
			
			Type vType = value.GetType();
			Type underlyingType = Enum.GetUnderlyingType (enumType);
			if (vType.IsEnum) {
				if (vType != enumType)
					throw new ArgumentException (string.Format(CultureInfo.InvariantCulture,
						"Object must be the same type as the enum. The type" +
						" passed in was {0}; the enum type was {1}.",
						vType.FullName, enumType.FullName));
			} else if (vType != underlyingType) {
				throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
					"Enum underlying type and the object must be the same type" +
					" or object. Type passed in was {0}; the enum underlying" +
					" type was {1}.", vType.FullName, underlyingType.FullName));
			}

			if (format.Length != 1)
				throw new FormatException ("Format String can be only \"G\",\"g\",\"X\"," + 
					"\"x\",\"F\",\"f\",\"D\" or \"d\".");

			char formatChar = format [0];
			string retVal;
			if ((formatChar == 'G' || formatChar == 'g')) {
				if (!enumType.IsDefined (typeof(FlagsAttribute), false)) {
					retVal = GetName (enumType, value);
					if (retVal == null)
						retVal = value.ToString();

					return retVal;
				}

				formatChar = 'f';
			}
			
			if ((formatChar == 'f' || formatChar == 'F'))
				return FormatFlags (enumType, value);

			retVal = String.Empty;
			switch (formatChar) {
			case 'X':
				retVal = FormatSpecifier_X (enumType, value, true);
				break;
			case 'x':
				retVal = FormatSpecifier_X (enumType, value, false);
				break;
			case 'D':
			case 'd':
				if (underlyingType == typeof (ulong)) {
					ulong ulongValue = Convert.ToUInt64 (value);
					retVal = ulongValue.ToString ();
				} else {
					long longValue = Convert.ToInt64 (value);
					retVal = longValue.ToString ();
				}
				break;
			default:
				throw new FormatException ("Format String can be only \"G\",\"g\",\"X\"," + 
					"\"x\",\"F\",\"f\",\"D\" or \"d\".");
			}
			return retVal;
		}
示例#8
0
文件: Array.cs 项目: mt-yu/mono
        private static void Copy(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, bool reliable)
        {
            if (sourceArray == null)
            {
                throw new ArgumentNullException(nameof(sourceArray));
            }

            if (destinationArray == null)
            {
                throw new ArgumentNullException(nameof(destinationArray));
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), "Value has to be >= 0.");
            }

            if (sourceArray.Rank != destinationArray.Rank)
            {
                throw new RankException(SR.Rank_MultiDimNotSupported);
            }

            if (sourceIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Value has to be >= 0.");
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Value has to be >= 0.");
            }

            if (FastCopy(sourceArray, sourceIndex, destinationArray, destinationIndex, length))
            {
                return;
            }

            int source_pos = sourceIndex - sourceArray.GetLowerBound(0);
            int dest_pos   = destinationIndex - destinationArray.GetLowerBound(0);

            if (source_pos < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Index was less than the array's lower bound in the first dimension.");
            }

            if (dest_pos < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Index was less than the array's lower bound in the first dimension.");
            }

            // re-ordered to avoid possible integer overflow
            if (source_pos > sourceArray.Length - length)
            {
                throw new ArgumentException(SR.Arg_LongerThanSrcArray, nameof(sourceArray));
            }

            if (dest_pos > destinationArray.Length - length)
            {
                throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds", nameof(destinationArray));
            }

            Type src_type    = sourceArray.GetType().GetElementType() !;
            Type dst_type    = destinationArray.GetType().GetElementType() !;
            var  dst_type_vt = dst_type.IsValueType && Nullable.GetUnderlyingType(dst_type) == null;

            bool src_is_enum = src_type.IsEnum;
            bool dst_is_enum = dst_type.IsEnum;

            if (src_is_enum)
            {
                src_type = Enum.GetUnderlyingType(src_type);
            }
            if (dst_is_enum)
            {
                dst_type = Enum.GetUnderlyingType(dst_type);
            }

            if (reliable)
            {
                if (!dst_type.Equals(src_type) &&
                    !(dst_type.IsPrimitive && src_type.IsPrimitive && CanChangePrimitive(dst_type, src_type, true)))
                {
                    throw new ArrayTypeMismatchException(SR.ArrayTypeMismatch_CantAssignType);
                }
            }
            else
            {
                if (!CanAssignArrayElement(src_type, dst_type))
                {
                    throw new ArrayTypeMismatchException(SR.ArrayTypeMismatch_CantAssignType);
                }
            }

            if (!Object.ReferenceEquals(sourceArray, destinationArray) || source_pos > dest_pos)
            {
                for (int i = 0; i < length; i++)
                {
                    Object srcval = sourceArray.GetValueImpl(source_pos + i);

                    if (!src_type.IsValueType && dst_is_enum)
                    {
                        throw new InvalidCastException(SR.InvalidCast_DownCastArrayElement);
                    }

                    if (dst_type_vt && (srcval == null || (src_type == typeof(object) && srcval.GetType() != dst_type)))
                    {
                        throw new InvalidCastException();
                    }

                    try {
                        destinationArray.SetValueRelaxedImpl(srcval, dest_pos + i);
                    } catch (ArgumentException) {
                        throw CreateArrayTypeMismatchException();
                    }
                }
            }
            else
            {
                for (int i = length - 1; i >= 0; i--)
                {
                    Object srcval = sourceArray.GetValueImpl(source_pos + i);

                    try {
                        destinationArray.SetValueRelaxedImpl(srcval, dest_pos + i);
                    } catch (ArgumentException) {
                        throw CreateArrayTypeMismatchException();
                    }
                }
            }
        }