Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SignatureTypeExtensions.cs
Go to the documentation of this file.
2
3namespace System.Reflection;
4
5internal static class SignatureTypeExtensions
6{
7 public static bool MatchesParameterTypeExactly(this Type pattern, ParameterInfo parameter)
8 {
9 if (pattern is SignatureType pattern2)
10 {
11 return pattern2.MatchesExactly(parameter.ParameterType);
12 }
13 return (object)pattern == parameter.ParameterType;
14 }
15
16 internal static bool MatchesExactly(this SignatureType pattern, Type actual)
17 {
18 if (pattern.IsSZArray)
19 {
20 if (actual.IsSZArray)
21 {
22 return pattern.ElementType.MatchesExactly(actual.GetElementType());
23 }
24 return false;
25 }
26 if (pattern.IsVariableBoundArray)
27 {
28 if (actual.IsVariableBoundArray && pattern.GetArrayRank() == actual.GetArrayRank())
29 {
30 return pattern.ElementType.MatchesExactly(actual.GetElementType());
31 }
32 return false;
33 }
34 if (pattern.IsByRef)
35 {
36 if (actual.IsByRef)
37 {
38 return pattern.ElementType.MatchesExactly(actual.GetElementType());
39 }
40 return false;
41 }
42 if (pattern.IsPointer)
43 {
44 if (actual.IsPointer)
45 {
46 return pattern.ElementType.MatchesExactly(actual.GetElementType());
47 }
48 return false;
49 }
50 if (pattern.IsConstructedGenericType)
51 {
52 if (!actual.IsConstructedGenericType)
53 {
54 return false;
55 }
56 if (!(pattern.GetGenericTypeDefinition() == actual.GetGenericTypeDefinition()))
57 {
58 return false;
59 }
60 Type[] genericTypeArguments = pattern.GenericTypeArguments;
61 Type[] genericTypeArguments2 = actual.GenericTypeArguments;
62 int num = genericTypeArguments.Length;
63 if (num != genericTypeArguments2.Length)
64 {
65 return false;
66 }
67 for (int i = 0; i < num; i++)
68 {
69 Type type = genericTypeArguments[i];
70 if (type is SignatureType pattern2)
71 {
72 if (!pattern2.MatchesExactly(genericTypeArguments2[i]))
73 {
74 return false;
75 }
76 }
77 else if (type != genericTypeArguments2[i])
78 {
79 return false;
80 }
81 }
82 return true;
83 }
84 if (pattern.IsGenericMethodParameter)
85 {
86 if (!actual.IsGenericMethodParameter)
87 {
88 return false;
89 }
90 if (pattern.GenericParameterPosition != actual.GenericParameterPosition)
91 {
92 return false;
93 }
94 return true;
95 }
96 return false;
97 }
98
99 internal static Type TryResolveAgainstGenericMethod(this SignatureType signatureType, MethodInfo genericMethod)
100 {
101 return signatureType.TryResolve(genericMethod.GetGenericArguments());
102 }
103
104 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Used to find matching method overloads. Only used for assignability checks.")]
105 private static Type TryResolve(this SignatureType signatureType, Type[] genericMethodParameters)
106 {
107 if (signatureType.IsSZArray)
108 {
109 Type type = signatureType.ElementType.TryResolve(genericMethodParameters);
110 if ((object)type == null)
111 {
112 return null;
113 }
114 return type.TryMakeArrayType();
115 }
116 if (signatureType.IsVariableBoundArray)
117 {
118 Type type2 = signatureType.ElementType.TryResolve(genericMethodParameters);
119 if ((object)type2 == null)
120 {
121 return null;
122 }
123 return type2.TryMakeArrayType(signatureType.GetArrayRank());
124 }
125 if (signatureType.IsByRef)
126 {
127 Type type3 = signatureType.ElementType.TryResolve(genericMethodParameters);
128 if ((object)type3 == null)
129 {
130 return null;
131 }
132 return type3.TryMakeByRefType();
133 }
134 if (signatureType.IsPointer)
135 {
136 Type type4 = signatureType.ElementType.TryResolve(genericMethodParameters);
137 if ((object)type4 == null)
138 {
139 return null;
140 }
141 return type4.TryMakePointerType();
142 }
143 if (signatureType.IsConstructedGenericType)
144 {
145 Type[] genericTypeArguments = signatureType.GenericTypeArguments;
146 int num = genericTypeArguments.Length;
147 Type[] array = new Type[num];
148 for (int i = 0; i < num; i++)
149 {
150 Type type5 = genericTypeArguments[i];
151 if (type5 is SignatureType signatureType2)
152 {
153 array[i] = signatureType2.TryResolve(genericMethodParameters);
154 if (array[i] == null)
155 {
156 return null;
157 }
158 }
159 else
160 {
161 array[i] = type5;
162 }
163 }
164 return signatureType.GetGenericTypeDefinition().TryMakeGenericType(array);
165 }
166 if (signatureType.IsGenericMethodParameter)
167 {
168 int genericParameterPosition = signatureType.GenericParameterPosition;
169 if (genericParameterPosition >= genericMethodParameters.Length)
170 {
171 return null;
172 }
173 return genericMethodParameters[genericParameterPosition];
174 }
175 return null;
176 }
177
178 private static Type TryMakeArrayType(this Type type)
179 {
180 try
181 {
182 return type.MakeArrayType();
183 }
184 catch
185 {
186 return null;
187 }
188 }
189
190 private static Type TryMakeArrayType(this Type type, int rank)
191 {
192 try
193 {
194 return type.MakeArrayType(rank);
195 }
196 catch
197 {
198 return null;
199 }
200 }
201
202 private static Type TryMakeByRefType(this Type type)
203 {
204 try
205 {
206 return type.MakeByRefType();
207 }
208 catch
209 {
210 return null;
211 }
212 }
213
214 private static Type TryMakePointerType(this Type type)
215 {
216 try
217 {
218 return type.MakePointerType();
219 }
220 catch
221 {
222 return null;
223 }
224 }
225
226 [RequiresUnreferencedCode("Wrapper around MakeGenericType which itself has RequiresUnreferencedCode")]
227 private static Type TryMakeGenericType(this Type type, Type[] instantiation)
228 {
229 try
230 {
231 return type.MakeGenericType(instantiation);
232 }
233 catch
234 {
235 return null;
236 }
237 }
238}
override Type[] GetGenericArguments()
Definition MethodInfo.cs:30
static bool MatchesExactly(this SignatureType pattern, Type actual)
static Type TryMakeGenericType(this Type type, Type[] instantiation)
static Type TryMakeArrayType(this Type type, int rank)
static bool MatchesParameterTypeExactly(this Type pattern, ParameterInfo parameter)
static Type TryResolve(this SignatureType signatureType, Type[] genericMethodParameters)
static Type TryResolveAgainstGenericMethod(this SignatureType signatureType, MethodInfo genericMethod)
override Type GetGenericTypeDefinition()
Type? GetElementType()
bool IsPointer
Definition Type.cs:75
virtual Type[] GenericTypeArguments
Definition Type.cs:146
virtual bool IsConstructedGenericType
Definition Type.cs:78
virtual int GenericParameterPosition
Definition Type.cs:158
virtual bool IsVariableBoundArray
Definition Type.cs:124
virtual int GetArrayRank()
Definition Type.cs:490
virtual bool IsGenericMethodParameter
Definition Type.cs:100
virtual bool IsSZArray
Definition Type.cs:116
bool IsByRef
Definition Type.cs:73
virtual Type GetGenericTypeDefinition()
Definition Type.cs:495