Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EnumDataTypeAttribute.cs
Go to the documentation of this file.
2
4
5[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
7{
8 public Type EnumType { get; }
9
10 public EnumDataTypeAttribute(Type enumType)
11 : base("Enumeration")
12 {
13 EnumType = enumType;
14 }
15
16 public override bool IsValid(object? value)
17 {
18 if (EnumType == null)
19 {
21 }
22 if (!EnumType.IsEnum)
23 {
25 }
26 if (value == null)
27 {
28 return true;
29 }
30 string text = value as string;
31 if (text != null && text.Length == 0)
32 {
33 return true;
34 }
36 if (type.IsEnum && EnumType != type)
37 {
38 return false;
39 }
40 if (!type.IsValueType && type != typeof(string))
41 {
42 return false;
43 }
44 if (type == typeof(bool) || type == typeof(float) || type == typeof(double) || type == typeof(decimal) || type == typeof(char))
45 {
46 return false;
47 }
48 object obj;
49 if (type.IsEnum)
50 {
51 obj = value;
52 }
53 else
54 {
55 try
56 {
57 obj = ((text != null) ? Enum.Parse(EnumType, text, ignoreCase: false) : Enum.ToObject(EnumType, value));
58 }
59 catch (ArgumentException)
60 {
61 return false;
62 }
63 }
65 {
66 string underlyingTypeValueString = GetUnderlyingTypeValueString(EnumType, obj);
67 string value2 = obj.ToString();
68 return !underlyingTypeValueString.Equals(value2);
69 }
70 return Enum.IsDefined(EnumType, obj);
71 }
72
73 private static bool IsEnumTypeInFlagsMode(Type enumType)
74 {
75 return enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
76 }
77
78 private static string GetUnderlyingTypeValueString(Type enumType, object enumValue)
79 {
80 return Convert.ChangeType(enumValue, Enum.GetUnderlyingType(enumType), CultureInfo.InvariantCulture).ToString();
81 }
82}
static string GetUnderlyingTypeValueString(Type enumType, object enumValue)
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
static bool IsDefined(Type enumType, object value)
Definition Enum.cs:359
static object Parse(Type enumType, string value)
Definition Enum.cs:368
static object ToObject(Type enumType, object value)
Definition Enum.cs:874
static CultureInfo InvariantCulture
bool IsDefined(Type attributeType, bool inherit)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string EnumDataTypeAttribute_TypeNeedsToBeAnEnum
Definition SR.cs:52
static string EnumDataTypeAttribute_TypeCannotBeNull
Definition SR.cs:50
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
virtual bool IsEnum
Definition Type.cs:227
string? FullName
Definition Type.cs:47