-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
RuntimeParameterInfo.cs
542 lines (453 loc) · 20.8 KB
/
RuntimeParameterInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using MdToken = System.Reflection.MetadataToken;
namespace System.Reflection
{
internal sealed unsafe class RuntimeParameterInfo : ParameterInfo
{
#region Static Members
internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
{
Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);
return GetParameters(method, member, sig, out _, fetchReturnParameter: false);
}
internal static ParameterInfo GetReturnParameter(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
{
Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo);
GetParameters(method, member, sig, out ParameterInfo? returnParameter, fetchReturnParameter: true);
return returnParameter!;
}
private static ParameterInfo[] GetParameters(
IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo? returnParameter, bool fetchReturnParameter)
{
returnParameter = null;
int sigArgCount = sig.Arguments.Length;
ParameterInfo[] args =
fetchReturnParameter ? null! :
sigArgCount == 0 ? Array.Empty<ParameterInfo>() :
new ParameterInfo[sigArgCount];
int tkMethodDef = RuntimeMethodHandle.GetMethodDef(methodHandle);
int cParamDefs = 0;
// Not all methods have tokens. Arrays, pointers and byRef types do not have tokens as they
// are generated on the fly by the runtime.
if (!MdToken.IsNullToken(tkMethodDef))
{
MetadataImport scope = RuntimeTypeHandle.GetMetadataImport(RuntimeMethodHandle.GetDeclaringType(methodHandle));
scope.EnumParams(tkMethodDef, out MetadataEnumResult tkParamDefs);
cParamDefs = tkParamDefs.Length;
// Not all parameters have tokens. Parameters may have no token
// if they have no name and no attributes.
if (cParamDefs > sigArgCount + 1 /* return type */)
throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
for (int i = 0; i < cParamDefs; i++)
{
#region Populate ParameterInfos
int tkParamDef = tkParamDefs[i];
scope.GetParamDefProps(tkParamDef, out int position, out ParameterAttributes attr);
position--;
if (fetchReturnParameter && position == -1)
{
// more than one return parameter?
if (returnParameter != null)
throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
returnParameter = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
}
else if (!fetchReturnParameter && position >= 0)
{
// position beyong sigArgCount?
if (position >= sigArgCount)
throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch);
args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member);
}
#endregion
}
}
// Fill in empty ParameterInfos for those without tokens
if (fetchReturnParameter)
{
returnParameter ??= new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, -1, (ParameterAttributes)0, member);
}
else
{
if (cParamDefs < args.Length + 1)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i] != null)
continue;
args[i] = new RuntimeParameterInfo(sig, MetadataImport.EmptyImport, 0, i, (ParameterAttributes)0, member);
}
}
}
return args;
}
#endregion
#region Private Statics
private static readonly Type s_DecimalConstantAttributeType = typeof(DecimalConstantAttribute);
private static readonly Type s_CustomConstantAttributeType = typeof(CustomConstantAttribute);
#endregion
#region Private Data Members
private int m_tkParamDef;
private MetadataImport m_scope;
private Signature? m_signature;
private volatile bool m_nameIsCached;
private readonly bool m_noMetadata;
private bool m_noDefaultValue;
private MethodBase? m_originalMember;
#endregion
#region Internal Properties
internal MethodBase DefiningMethod
{
get
{
MethodBase? result = m_originalMember ?? MemberImpl as MethodBase;
Debug.Assert(result != null);
return result;
}
}
#endregion
#region Internal Methods
internal void SetName(string? name)
{
NameImpl = name;
}
internal void SetAttributes(ParameterAttributes attributes)
{
AttrsImpl = attributes;
}
#endregion
#region Constructor
// used by RuntimePropertyInfo
internal RuntimeParameterInfo(RuntimeParameterInfo accessor, RuntimePropertyInfo property)
: this(accessor, (MemberInfo)property)
{
m_signature = property.Signature;
}
private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member)
{
// Change ownership
MemberImpl = member;
// The original owner should always be a method, because this method is only used to
// change the owner from a method to a property.
m_originalMember = accessor.MemberImpl as MethodBase;
Debug.Assert(m_originalMember != null);
// Populate all the caches -- we inherit this behavior from RTM
NameImpl = accessor.Name;
m_nameIsCached = true;
ClassImpl = accessor.ParameterType;
PositionImpl = accessor.Position;
AttrsImpl = accessor.Attributes;
// Strictly speeking, property's don't contain parameter tokens
// However we need this to make ca's work... oh well...
m_tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken;
m_scope = accessor.m_scope;
}
private RuntimeParameterInfo(
Signature signature, MetadataImport scope, int tkParamDef,
int position, ParameterAttributes attributes, MemberInfo member)
{
Debug.Assert(member != null);
Debug.Assert(MdToken.IsNullToken(tkParamDef) == scope.Equals(MetadataImport.EmptyImport));
Debug.Assert(MdToken.IsNullToken(tkParamDef) || MdToken.IsTokenOfType(tkParamDef, MetadataTokenType.ParamDef));
PositionImpl = position;
MemberImpl = member;
m_signature = signature;
m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef;
m_scope = scope;
AttrsImpl = attributes;
ClassImpl = null;
NameImpl = null;
}
// ctor for no metadata MethodInfo in the DynamicMethod and RuntimeMethodInfo cases
internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType, int position)
{
MemberImpl = owner;
NameImpl = name;
m_nameIsCached = true;
m_noMetadata = true;
ClassImpl = parameterType;
PositionImpl = position;
AttrsImpl = ParameterAttributes.None;
m_tkParamDef = (int)MetadataTokenType.ParamDef;
m_scope = MetadataImport.EmptyImport;
}
#endregion
#region Public Methods
public override Type ParameterType
{
get
{
// only instance of ParameterInfo has ClassImpl, all its subclasses don't
if (ClassImpl == null)
{
Debug.Assert(m_signature != null);
RuntimeType parameterType;
if (PositionImpl == -1)
parameterType = m_signature.ReturnType;
else
parameterType = m_signature.Arguments[PositionImpl];
Debug.Assert(parameterType != null);
// different thread could only write ClassImpl to the same value, so a race condition is not a problem here
ClassImpl = parameterType;
}
return ClassImpl;
}
}
public override string? Name
{
get
{
if (!m_nameIsCached)
{
if (!MdToken.IsNullToken(m_tkParamDef))
{
string name = m_scope.GetName(m_tkParamDef).ToString();
NameImpl = name;
}
// other threads could only write it to true, so a race condition is OK
// this field is volatile, so the write ordering is guaranteed
m_nameIsCached = true;
}
// name may be null
return NameImpl;
}
}
public override bool HasDefaultValue
{
get
{
if (m_noMetadata || m_noDefaultValue)
return false;
object? defaultValue = GetDefaultValueInternal(false);
return defaultValue != DBNull.Value;
}
}
public override object? DefaultValue => GetDefaultValue(false);
public override object? RawDefaultValue => GetDefaultValue(true);
private object? GetDefaultValue(bool raw)
{
// OLD COMMENT (Is this even true?)
// Cannot cache because default value could be non-agile user defined enumeration.
// OLD COMMENT ends
if (m_noMetadata)
return null;
// for dynamic method we pretend to have cached the value so we do not go to metadata
object? defaultValue = GetDefaultValueInternal(raw);
if (defaultValue == DBNull.Value)
{
#region Handle case if no default value was found
if (IsOptional)
{
// If the argument is marked as optional then the default value is Missing.Value.
defaultValue = Type.Missing;
}
#endregion
}
return defaultValue;
}
// returns DBNull.Value if the parameter doesn't have a default value
private object? GetDefaultValueInternal(bool raw)
{
Debug.Assert(!m_noMetadata);
if (m_noDefaultValue)
return DBNull.Value;
object? defaultValue = null;
// Why check the parameter type only for DateTime and only for the ctor arguments?
// No check on the parameter type is done for named args and for Decimal.
// We should move this after MdToken.IsNullToken(m_tkParamDef) and combine it
// with the other custom attribute logic. But will that be a breaking change?
// For a DateTime parameter on which both an md constant and a ca constant are set,
// which one should win?
if (ParameterType == typeof(DateTime))
{
if (raw)
{
CustomAttributeTypedArgument value =
RuntimeCustomAttributeData.Filter(
RuntimeCustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0);
if (value.ArgumentType != null)
return new DateTime((long)value.Value!);
}
else
{
object[] dt = GetCustomAttributes(typeof(DateTimeConstantAttribute), false);
if (dt != null && dt.Length != 0)
return ((DateTimeConstantAttribute)dt[0]).Value;
}
}
#region Look for a default value in metadata
if (!MdToken.IsNullToken(m_tkParamDef))
{
// This will return DBNull.Value if no constant value is defined on m_tkParamDef in the metadata.
defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.TypeHandle, raw);
}
#endregion
if (defaultValue == DBNull.Value)
{
#region Look for a default value in the custom attributes
if (raw)
{
foreach (CustomAttributeData attr in CustomAttributeData.GetCustomAttributes(this))
{
Type? attrType = attr.Constructor.DeclaringType;
if (attrType == typeof(DateTimeConstantAttribute))
{
defaultValue = GetRawDateTimeConstant(attr);
}
else if (attrType == typeof(DecimalConstantAttribute))
{
defaultValue = GetRawDecimalConstant(attr);
}
else if (attrType!.IsSubclassOf(s_CustomConstantAttributeType))
{
defaultValue = GetRawConstant(attr);
}
}
}
else
{
object[] CustomAttrs = GetCustomAttributes(s_CustomConstantAttributeType, false);
if (CustomAttrs.Length != 0)
{
defaultValue = ((CustomConstantAttribute)CustomAttrs[0]).Value;
}
else
{
CustomAttrs = GetCustomAttributes(s_DecimalConstantAttributeType, false);
if (CustomAttrs.Length != 0)
{
defaultValue = ((DecimalConstantAttribute)CustomAttrs[0]).Value;
}
}
}
#endregion
}
if (defaultValue == DBNull.Value)
m_noDefaultValue = true;
return defaultValue;
}
private static decimal GetRawDecimalConstant(CustomAttributeData attr)
{
Debug.Assert(attr.Constructor.DeclaringType == typeof(DecimalConstantAttribute));
foreach (CustomAttributeNamedArgument namedArgument in attr.NamedArguments)
{
if (namedArgument.MemberInfo.Name.Equals("Value"))
{
// This is not possible because Decimal cannot be represented directly in the metadata.
Debug.Fail("Decimal cannot be represented directly in the metadata.");
return (decimal)namedArgument.TypedValue.Value!;
}
}
ParameterInfo[] parameters = attr.Constructor.GetParameters();
Debug.Assert(parameters.Length == 5);
System.Collections.Generic.IList<CustomAttributeTypedArgument> args = attr.ConstructorArguments;
Debug.Assert(args.Count == 5);
if (parameters[2].ParameterType == typeof(uint))
{
// DecimalConstantAttribute(byte scale, byte sign, uint hi, uint mid, uint low)
int low = (int)(uint)args[4].Value!;
int mid = (int)(uint)args[3].Value!;
int hi = (int)(uint)args[2].Value!;
byte sign = (byte)args[1].Value!;
byte scale = (byte)args[0].Value!;
return new decimal(low, mid, hi, sign != 0, scale);
}
else
{
// DecimalConstantAttribute(byte scale, byte sign, int hi, int mid, int low)
int low = (int)args[4].Value!;
int mid = (int)args[3].Value!;
int hi = (int)args[2].Value!;
byte sign = (byte)args[1].Value!;
byte scale = (byte)args[0].Value!;
return new decimal(low, mid, hi, sign != 0, scale);
}
}
private static DateTime GetRawDateTimeConstant(CustomAttributeData attr)
{
Debug.Assert(attr.Constructor.DeclaringType == typeof(DateTimeConstantAttribute));
Debug.Assert(attr.ConstructorArguments.Count == 1);
foreach (CustomAttributeNamedArgument namedArgument in attr.NamedArguments)
{
if (namedArgument.MemberInfo.Name.Equals("Value"))
{
return new DateTime((long)namedArgument.TypedValue.Value!);
}
}
// Look at the ctor argument if the "Value" property was not explicitly defined.
return new DateTime((long)attr.ConstructorArguments[0].Value!);
}
private static object? GetRawConstant(CustomAttributeData attr)
{
foreach (CustomAttributeNamedArgument namedArgument in attr.NamedArguments)
{
if (namedArgument.MemberInfo.Name.Equals("Value"))
return namedArgument.TypedValue.Value;
}
// Return DBNull to indicate that no default value is available.
// Not to be confused with a null return which indicates a null default value.
return DBNull.Value;
}
internal RuntimeModule? GetRuntimeModule()
{
RuntimeMethodInfo? method = Member as RuntimeMethodInfo;
RuntimeConstructorInfo? constructor = Member as RuntimeConstructorInfo;
RuntimePropertyInfo? property = Member as RuntimePropertyInfo;
if (method != null)
return method.GetRuntimeModule();
else if (constructor != null)
return constructor.GetRuntimeModule();
else if (property != null)
return property.GetRuntimeModule();
else
return null;
}
public override int MetadataToken => m_tkParamDef;
public override Type[] GetRequiredCustomModifiers()
{
return m_signature is null ?
Type.EmptyTypes :
m_signature.GetCustomModifiers(PositionImpl + 1, true);
}
public override Type[] GetOptionalCustomModifiers()
{
return m_signature is null ?
Type.EmptyTypes :
m_signature.GetCustomModifiers(PositionImpl + 1, false);
}
#endregion
#region ICustomAttributeProvider
public override object[] GetCustomAttributes(bool inherit)
{
if (MdToken.IsNullToken(m_tkParamDef))
return Array.Empty<object>();
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
if (MdToken.IsNullToken(m_tkParamDef))
return CustomAttribute.CreateAttributeArrayHelper(attributeRuntimeType, 0);
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
public override bool IsDefined(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
if (MdToken.IsNullToken(m_tkParamDef))
return false;
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.IsDefined(this, attributeRuntimeType);
}
public override IList<CustomAttributeData> GetCustomAttributesData()
{
return RuntimeCustomAttributeData.GetCustomAttributesInternal(this);
}
#endregion
}
}