Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReflectionExtensions.cs
Go to the documentation of this file.
6
8
9internal static class ReflectionExtensions
10{
11 private static readonly Type s_nullableType = typeof(Nullable<>);
12
14 {
15 if ((object)objectType == null)
16 {
17 objectType = typeof(object);
18 }
19 Type type2 = type;
20 while (type2 != null && type2 != typeof(object))
21 {
22 if (type2.IsGenericType)
23 {
24 Type genericTypeDefinition = type2.GetGenericTypeDefinition();
26 {
27 return type2;
28 }
29 }
30 type2 = type2.BaseType;
31 }
32 return null;
33 }
34
35 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", Justification = "The 'interfaceType' must exist and so trimmer kept it. In which case It also kept it on any type which implements it. The below call to GetInterfaces may return fewer results when trimmed but it will return the 'interfaceType' if the type implemented it, even after trimming.")]
37 {
38 Type type2 = type;
39 if (type2.IsGenericType)
40 {
41 type2 = type2.GetGenericTypeDefinition();
42 }
43 if (type2 == interfaceType)
44 {
45 return type;
46 }
47 Type[] interfaces = type.GetInterfaces();
48 foreach (Type type3 in interfaces)
49 {
50 if (type3.IsGenericType)
51 {
52 Type genericTypeDefinition = type3.GetGenericTypeDefinition();
54 {
55 return type3;
56 }
57 }
58 }
59 return null;
60 }
61
62 public static bool IsImmutableDictionaryType(this Type type, bool sourceGenType = false)
63 {
64 if (!type.IsGenericType || !type.Assembly.FullName.StartsWith("System.Collections.Immutable", StringComparison.Ordinal))
65 {
66 return false;
67 }
69 {
70 case "System.Collections.Immutable.ImmutableDictionary`2":
71 case "System.Collections.Immutable.IImmutableDictionary`2":
72 case "System.Collections.Immutable.ImmutableSortedDictionary`2":
73 return true;
74 default:
75 return false;
76 }
77 }
78
79 public static bool IsImmutableEnumerableType(this Type type, bool sourceGenType = false)
80 {
81 if (!type.IsGenericType || !type.Assembly.FullName.StartsWith("System.Collections.Immutable", StringComparison.Ordinal))
82 {
83 return false;
84 }
86 {
87 case "System.Collections.Immutable.ImmutableArray`1":
88 case "System.Collections.Immutable.ImmutableList`1":
89 case "System.Collections.Immutable.IImmutableList`1":
90 case "System.Collections.Immutable.ImmutableStack`1":
91 case "System.Collections.Immutable.IImmutableStack`1":
92 case "System.Collections.Immutable.ImmutableQueue`1":
93 case "System.Collections.Immutable.IImmutableQueue`1":
94 case "System.Collections.Immutable.ImmutableSortedSet`1":
95 case "System.Collections.Immutable.ImmutableHashSet`1":
96 case "System.Collections.Immutable.IImmutableSet`1":
97 return true;
98 default:
99 return false;
100 }
101 }
102
103 public static string GetImmutableDictionaryConstructingTypeName(this Type type, bool sourceGenType = false)
104 {
106 {
107 case "System.Collections.Immutable.ImmutableDictionary`2":
108 case "System.Collections.Immutable.IImmutableDictionary`2":
109 return "System.Collections.Immutable.ImmutableDictionary";
110 case "System.Collections.Immutable.ImmutableSortedDictionary`2":
111 return "System.Collections.Immutable.ImmutableSortedDictionary";
112 default:
113 return null;
114 }
115 }
116
117 public static string GetImmutableEnumerableConstructingTypeName(this Type type, bool sourceGenType = false)
118 {
120 {
121 case "System.Collections.Immutable.ImmutableArray`1":
122 return "System.Collections.Immutable.ImmutableArray";
123 case "System.Collections.Immutable.ImmutableList`1":
124 case "System.Collections.Immutable.IImmutableList`1":
125 return "System.Collections.Immutable.ImmutableList";
126 case "System.Collections.Immutable.ImmutableStack`1":
127 case "System.Collections.Immutable.IImmutableStack`1":
128 return "System.Collections.Immutable.ImmutableStack";
129 case "System.Collections.Immutable.ImmutableQueue`1":
130 case "System.Collections.Immutable.IImmutableQueue`1":
131 return "System.Collections.Immutable.ImmutableQueue";
132 case "System.Collections.Immutable.ImmutableSortedSet`1":
133 return "System.Collections.Immutable.ImmutableSortedSet";
134 case "System.Collections.Immutable.ImmutableHashSet`1":
135 case "System.Collections.Immutable.IImmutableSet`1":
136 return "System.Collections.Immutable.ImmutableHashSet";
137 default:
138 return null;
139 }
140 }
141
143 {
145 }
146
148 {
149 Type genericTypeDefinition = genericType.GetGenericTypeDefinition();
150 if (!sourceGenType)
151 {
152 return genericTypeDefinition.FullName;
153 }
155 }
156
158 {
159 string fullName = genericTypeDef.FullName;
160 return fullName[..(fullName.IndexOf("`") + 2)];
161 }
162
163 public static bool IsVirtual(this PropertyInfo propertyInfo)
164 {
165 if (propertyInfo != null)
166 {
167 MethodInfo? getMethod = propertyInfo.GetMethod;
168 if ((object)getMethod == null || !getMethod.IsVirtual)
169 {
170 return propertyInfo.SetMethod?.IsVirtual ?? false;
171 }
172 return true;
173 }
174 return false;
175 }
176
177 public static bool IsKeyValuePair(this Type type, Type keyValuePairType = null)
178 {
179 if (!type.IsGenericType)
180 {
181 return false;
182 }
183 if ((object)keyValuePairType == null)
184 {
186 }
187 Type genericTypeDefinition = type.GetGenericTypeDefinition();
189 }
190
192 {
196 ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
197 if (constructors.Length == 1)
198 {
200 }
203 {
205 {
206 if (constructorInfo != null)
207 {
208 deserializationCtor = null;
209 return false;
210 }
212 }
213 else if (constructorInfo4.GetParameters().Length == 0)
214 {
216 }
217 }
219 constructors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
222 {
224 {
225 if (constructorInfo5 != null)
226 {
227 deserializationCtor = null;
228 return false;
229 }
231 }
232 }
233 if (useDefaultCtorInAnnotatedStructs && type.IsValueType && constructorInfo == null)
234 {
235 deserializationCtor = null;
236 return true;
237 }
239 return true;
240 }
241
242 public static object GetDefaultValue(this ParameterInfo parameterInfo)
243 {
244 object defaultValue = parameterInfo.DefaultValue;
245 if (defaultValue == DBNull.Value && parameterInfo.ParameterType != typeof(DBNull))
246 {
247 return null;
248 }
249 return defaultValue;
250 }
251
252 [MethodImpl(MethodImplOptions.AggressiveInlining)]
253 public static bool IsNullableOfT(this Type type)
254 {
255 if (type.IsGenericType)
256 {
257 return type.GetGenericTypeDefinition() == s_nullableType;
258 }
259 return false;
260 }
261
262 [MethodImpl(MethodImplOptions.AggressiveInlining)]
263 public static bool CanBeNull(this Type type)
264 {
265 if (type.IsValueType)
266 {
267 return type.IsNullableOfT();
268 }
269 return true;
270 }
271
272 public static bool IsAssignableFromInternal(this Type type, Type from)
273 {
274 if (from.IsNullableOfT() && type.IsInterface)
275 {
276 return type.IsAssignableFrom(from.GetGenericArguments()[0]);
277 }
278 return type.IsAssignableFrom(from);
279 }
280
282 {
283 return constructorInfo.GetCustomAttribute<JsonConstructorAttribute>() != null;
284 }
285}
static readonly DBNull Value
Definition DBNull.cs:8
virtual ? MethodInfo GetMethod
virtual ? MethodInfo SetMethod
static bool IsImmutableEnumerableType(this Type type, bool sourceGenType=false)
static string GetBaseNameFromGenericType(Type genericType, bool sourceGenType)
static bool IsKeyValuePair(this Type type, Type keyValuePairType=null)
static object GetDefaultValue(this ParameterInfo parameterInfo)
static Type GetCompatibleGenericInterface(this Type type, Type interfaceType)
static bool IsImmutableDictionaryType(this Type type, bool sourceGenType=false)
static bool HasJsonConstructorAttribute(ConstructorInfo constructorInfo)
static bool OpenGenericTypesHaveSamePrefix(Type t1, Type t2)
static string GetImmutableEnumerableConstructingTypeName(this Type type, bool sourceGenType=false)
static bool TryGetDeserializationConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] this Type type, bool useDefaultCtorInAnnotatedStructs, out ConstructorInfo deserializationCtor)
static bool IsVirtual(this PropertyInfo propertyInfo)
static bool IsAssignableFromInternal(this Type type, Type from)
static string GetImmutableDictionaryConstructingTypeName(this Type type, bool sourceGenType=false)
static Type GetCompatibleGenericBaseClass(this Type type, Type baseType, Type objectType=null, bool sourceGenType=false)
static string GetBaseNameFromGenericTypeDef(Type genericTypeDef)
Type[] GetInterfaces()