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

◆ BindToMethod() [2/2]

override MethodBase System.DefaultBinder.BindToMethod ( BindingFlags bindingAttr,
MethodBase[] match,
ref object[] args,
ParameterModifier[] modifiers,
CultureInfo cultureInfo,
string[] names,
out object state )
inlinesealedinherited

Definition at line 67 of file DefaultBinder.cs.

68 {
69 if (match == null || match.Length == 0)
70 {
71 throw new ArgumentException(SR.Arg_EmptyArray, "match");
72 }
73 MethodBase[] array = (MethodBase[])match.Clone();
74 state = null;
75 int[][] array2 = new int[array.Length][];
76 for (int i = 0; i < array.Length; i++)
77 {
78 ParameterInfo[] parametersNoCopy = array[i].GetParametersNoCopy();
79 array2[i] = new int[(parametersNoCopy.Length > args.Length) ? parametersNoCopy.Length : args.Length];
80 if (names == null)
81 {
82 for (int j = 0; j < args.Length; j++)
83 {
84 array2[i][j] = j;
85 }
86 }
87 else if (!CreateParamOrder(array2[i], parametersNoCopy, names))
88 {
89 array[i] = null;
90 }
91 }
92 Type[] array3 = new Type[array.Length];
93 Type[] array4 = new Type[args.Length];
94 for (int i = 0; i < args.Length; i++)
95 {
96 if (args[i] != null)
97 {
98 array4[i] = args[i].GetType();
99 }
100 }
101 int num = 0;
102 bool flag = (bindingAttr & BindingFlags.OptionalParamBinding) != 0;
103 for (int i = 0; i < array.Length; i++)
104 {
105 Type type = null;
106 if (array[i] == null)
107 {
108 continue;
109 }
110 ParameterInfo[] parametersNoCopy2 = array[i].GetParametersNoCopy();
111 if (parametersNoCopy2.Length == 0)
112 {
113 if (args.Length == 0 || (array[i].CallingConvention & CallingConventions.VarArgs) != 0)
114 {
115 array2[num] = array2[i];
116 array[num++] = array[i];
117 }
118 continue;
119 }
120 int j;
121 if (parametersNoCopy2.Length > args.Length)
122 {
123 for (j = args.Length; j < parametersNoCopy2.Length - 1 && parametersNoCopy2[j].DefaultValue != DBNull.Value; j++)
124 {
125 }
126 if (j != parametersNoCopy2.Length - 1)
127 {
128 continue;
129 }
130 if (parametersNoCopy2[j].DefaultValue == DBNull.Value)
131 {
132 if (!parametersNoCopy2[j].ParameterType.IsArray || !parametersNoCopy2[j].IsDefined(typeof(ParamArrayAttribute), inherit: true))
133 {
134 continue;
135 }
136 type = parametersNoCopy2[j].ParameterType.GetElementType();
137 }
138 }
139 else if (parametersNoCopy2.Length < args.Length)
140 {
141 int num2 = parametersNoCopy2.Length - 1;
142 if (!parametersNoCopy2[num2].ParameterType.IsArray || !parametersNoCopy2[num2].IsDefined(typeof(ParamArrayAttribute), inherit: true) || array2[i][num2] != num2)
143 {
144 continue;
145 }
146 type = parametersNoCopy2[num2].ParameterType.GetElementType();
147 }
148 else
149 {
150 int num3 = parametersNoCopy2.Length - 1;
151 if (parametersNoCopy2[num3].ParameterType.IsArray && parametersNoCopy2[num3].IsDefined(typeof(ParamArrayAttribute), inherit: true) && array2[i][num3] == num3 && !parametersNoCopy2[num3].ParameterType.IsAssignableFrom(array4[num3]))
152 {
153 type = parametersNoCopy2[num3].ParameterType.GetElementType();
154 }
155 }
156 int num4 = ((type != null) ? (parametersNoCopy2.Length - 1) : args.Length);
157 for (j = 0; j < num4; j++)
158 {
159 Type type2 = parametersNoCopy2[j].ParameterType;
160 if (type2.IsByRef)
161 {
162 type2 = type2.GetElementType();
163 }
164 if (type2 == array4[array2[i][j]] || (flag && args[array2[i][j]] == Type.Missing) || args[array2[i][j]] == null || type2 == typeof(object))
165 {
166 continue;
167 }
168 if (type2.IsPrimitive)
169 {
170 if (array4[array2[i][j]] == null || !CanChangePrimitive(args[array2[i][j]]?.GetType(), type2))
171 {
172 break;
173 }
174 }
175 else if (!(array4[array2[i][j]] == null) && !type2.IsAssignableFrom(array4[array2[i][j]]) && (!array4[array2[i][j]].IsCOMObject || !type2.IsInstanceOfType(args[array2[i][j]])))
176 {
177 break;
178 }
179 }
180 if (type != null && j == parametersNoCopy2.Length - 1)
181 {
182 for (; j < args.Length; j++)
183 {
184 if (type.IsPrimitive)
185 {
186 if (array4[j] == null || !CanChangePrimitive(args[j]?.GetType(), type))
187 {
188 break;
189 }
190 }
191 else if (!(array4[j] == null) && !type.IsAssignableFrom(array4[j]) && (!array4[j].IsCOMObject || !type.IsInstanceOfType(args[j])))
192 {
193 break;
194 }
195 }
196 }
197 if (j == args.Length)
198 {
199 array2[num] = array2[i];
200 array3[num] = type;
201 array[num++] = array[i];
202 }
203 }
204 switch (num)
205 {
206 case 0:
207 throw new MissingMethodException(SR.MissingMember);
208 case 1:
209 {
210 if (names != null)
211 {
212 state = new BinderState((int[])array2[0].Clone(), args.Length, array3[0] != null);
213 ReorderParams(array2[0], args);
214 }
215 ParameterInfo[] parametersNoCopy4 = array[0].GetParametersNoCopy();
216 if (parametersNoCopy4.Length == args.Length)
217 {
218 if (array3[0] != null)
219 {
220 object[] array8 = new object[parametersNoCopy4.Length];
221 int num8 = parametersNoCopy4.Length - 1;
222 Array.Copy(args, array8, num8);
223 array8[num8] = Array.CreateInstance(array3[0], 1);
224 ((Array)array8[num8]).SetValue(args[num8], 0);
225 args = array8;
226 }
227 }
228 else if (parametersNoCopy4.Length > args.Length)
229 {
230 object[] array9 = new object[parametersNoCopy4.Length];
231 int i;
232 for (i = 0; i < args.Length; i++)
233 {
234 array9[i] = args[i];
235 }
236 for (; i < parametersNoCopy4.Length - 1; i++)
237 {
238 array9[i] = parametersNoCopy4[i].DefaultValue;
239 }
240 if (array3[0] != null)
241 {
242 array9[i] = Array.CreateInstance(array3[0], 0);
243 }
244 else
245 {
246 array9[i] = parametersNoCopy4[i].DefaultValue;
247 }
248 args = array9;
249 }
250 else if ((array[0].CallingConvention & CallingConventions.VarArgs) == 0)
251 {
252 object[] array10 = new object[parametersNoCopy4.Length];
253 int num9 = parametersNoCopy4.Length - 1;
254 Array.Copy(args, array10, num9);
255 array10[num9] = Array.CreateInstance(array3[0], args.Length - num9);
256 Array.Copy(args, num9, (Array)array10[num9], 0, args.Length - num9);
257 args = array10;
258 }
259 return array[0];
260 }
261 default:
262 {
263 int num5 = 0;
264 bool flag2 = false;
265 for (int i = 1; i < num; i++)
266 {
267 switch (FindMostSpecificMethod(array[num5], array2[num5], array3[num5], array[i], array2[i], array3[i], array4, args))
268 {
269 case 0:
270 flag2 = true;
271 break;
272 case 2:
273 num5 = i;
274 flag2 = false;
275 break;
276 }
277 }
278 if (flag2)
279 {
280 throw new AmbiguousMatchException(SR.Arg_AmbiguousMatchException);
281 }
282 if (names != null)
283 {
284 state = new BinderState((int[])array2[num5].Clone(), args.Length, array3[num5] != null);
285 ReorderParams(array2[num5], args);
286 }
287 ParameterInfo[] parametersNoCopy3 = array[num5].GetParametersNoCopy();
288 if (parametersNoCopy3.Length == args.Length)
289 {
290 if (array3[num5] != null)
291 {
292 object[] array5 = new object[parametersNoCopy3.Length];
293 int num6 = parametersNoCopy3.Length - 1;
294 Array.Copy(args, array5, num6);
295 array5[num6] = Array.CreateInstance(array3[num5], 1);
296 ((Array)array5[num6]).SetValue(args[num6], 0);
297 args = array5;
298 }
299 }
300 else if (parametersNoCopy3.Length > args.Length)
301 {
302 object[] array6 = new object[parametersNoCopy3.Length];
303 int i;
304 for (i = 0; i < args.Length; i++)
305 {
306 array6[i] = args[i];
307 }
308 for (; i < parametersNoCopy3.Length - 1; i++)
309 {
310 array6[i] = parametersNoCopy3[i].DefaultValue;
311 }
312 if (array3[num5] != null)
313 {
314 array6[i] = Array.CreateInstance(array3[num5], 0);
315 }
316 else
317 {
318 array6[i] = parametersNoCopy3[i].DefaultValue;
319 }
320 args = array6;
321 }
322 else if ((array[num5].CallingConvention & CallingConventions.VarArgs) == 0)
323 {
324 object[] array7 = new object[parametersNoCopy3.Length];
325 int num7 = parametersNoCopy3.Length - 1;
326 Array.Copy(args, array7, num7);
327 array7[num7] = Array.CreateInstance(array3[num5], args.Length - num7);
328 Array.Copy(args, num7, (Array)array7[num7], 0, args.Length - num7);
329 args = array7;
330 }
331 return array[num5];
332 }
333 }
334 }
static int FindMostSpecificMethod(MethodBase m1, int[] paramOrder1, Type paramArrayType1, MethodBase m2, int[] paramOrder2, Type paramArrayType2, Type[] types, object[] args)
static bool CanChangePrimitive(Type source, Type target)
static void ReorderParams(int[] paramOrder, object[] vars)
static bool CreateParamOrder(int[] paramOrder, ParameterInfo[] pars, string[] names)
Type? GetElementType()

References System.SR.Arg_AmbiguousMatchException, System.SR.Arg_EmptyArray, System.array, System.Reflection.Array, System.DefaultBinder.CanChangePrimitive(), System.Array.Copy(), System.Array.CreateInstance(), System.DefaultBinder.CreateParamOrder(), System.Reflection.ParameterInfo.DefaultValue, System.DefaultBinder.FindMostSpecificMethod(), System.Type.GetElementType(), System.Type.GetType(), System.Type.IsAssignableFrom(), System.Type.IsByRef, System.Type.IsCOMObject, System.Reflection.ParameterInfo.IsDefined(), System.Type.IsInstanceOfType(), System.Type.IsPrimitive, System.match, System.Type.Missing, System.SR.MissingMember, System.Reflection.ParameterInfo.ParameterType, System.DefaultBinder.ReorderParams(), System.state, System.type, and System.DBNull.Value.