c3c55390d1e689b9e929a35c808756379722aa7d
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Diagnostics;
6
7 namespace System.Reflection.TypeLoading
8 {
9     // This helper class represents a combined return value info and parameter info array. It is motivated by the fact that
10     // the Reflection api surfaces "return types" as a "ParameterInfo at position -1." (and that the Ecma-335 metadata
11     // scheme similarly reuses the Parameter table for holding return type custom attributes and stuff.)
12     //
13     // Behaviorally, it acts like an array whose lower bound is -1 (where the "-1"th element is the "return" ParameterInfo).
14     //
15     // It also allows getting an array of the parameters (excluding the return value) without copying.
16     //
17     // The name is motivated by System.Reflection.Metadata using the name MethodSignature<T> for a similar concept and
18     // that it's far shorter than "ParametersAndReturnType".
19     internal sealed class MethodSig<T>
20     {
21         public T Return { get; private set; }
22         public T[] Parameters { get; }
23
24         public MethodSig(int parameterCount)
25         {
26             Debug.Assert(parameterCount >= 0);
27             Parameters = new T[parameterCount];
28         }
29
30         public T this[int position]
31         {
32             get
33             {
34                 Debug.Assert(position >= -1 && position < Parameters.Length);
35                 return position == -1 ? Return : Parameters[position];
36             }
37
38             set
39             {
40                 Debug.Assert(position >= -1 && position < Parameters.Length);
41                 if (position == -1)
42                 {
43                     Return = value;
44                 }
45                 else
46                 {
47                     Parameters[position] = value;
48                 }
49             }
50         }
51     }
52 }