Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ Format()

static string System.Enum.Format ( Type enumType,
object value,
string format )
inlinestatic

Definition at line 896 of file Enum.cs.

897 {
898 RuntimeType enumType2 = ValidateRuntimeType(enumType);
899 if (value == null)
900 {
901 throw new ArgumentNullException("value");
902 }
903 if (format == null)
904 {
905 throw new ArgumentNullException("format");
906 }
907 Type type = value.GetType();
908 if (type.IsEnum)
909 {
910 if (!type.IsEquivalentTo(enumType))
911 {
912 throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, type, enumType));
913 }
914 if (format.Length != 1)
915 {
916 throw new FormatException(SR.Format_InvalidEnumFormatSpecification);
917 }
918 return ((Enum)value).ToString(format);
919 }
920 Type underlyingType = GetUnderlyingType(enumType);
921 if (type != underlyingType)
922 {
923 throw new ArgumentException(SR.Format(SR.Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType, type, underlyingType));
924 }
925 if (format.Length == 1)
926 {
927 switch (format[0])
928 {
929 case 'G':
930 case 'g':
931 return InternalFormat(enumType2, ToUInt64(value)) ?? value.ToString();
932 case 'D':
933 case 'd':
934 return value.ToString();
935 case 'X':
936 case 'x':
937 return ValueToHexString(value);
938 case 'F':
939 case 'f':
940 return InternalFlagsFormat(enumType2, ToUInt64(value)) ?? value.ToString();
941 }
942 }
943 throw new FormatException(SR.Format_InvalidEnumFormatSpecification);
944 }
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
static string InternalFlagsFormat(RuntimeType enumType, ulong result)
Definition Enum.cs:172
static RuntimeType ValidateRuntimeType(Type enumType)
Definition Enum.cs:1269
string ValueToHexString()
Definition Enum.cs:89
ulong ToUInt64()
Definition Enum.cs:969
static string InternalFormat(RuntimeType enumType, ulong value)
Definition Enum.cs:162

References System.SR.Arg_EnumAndObjectMustBeSameType, System.SR.Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType, System.SR.Format(), System.format, System.SR.Format_InvalidEnumFormatSpecification, System.Type.GetType(), System.Enum.GetUnderlyingType(), System.Enum.InternalFlagsFormat(), System.Enum.InternalFormat(), System.Enum.ToUInt64(), System.type, System.Enum.ValidateRuntimeType(), System.value, and System.Enum.ValueToHexString().

Referenced by System.ComponentModel.EnumConverter.ConvertTo(), System.Xml.Serialization.SoapReflectionImporter.GetDefaultValue(), and System.Xml.Serialization.XmlReflectionImporter.GetDefaultValue().