3
3
4
4
using System . Buffers . Binary ;
5
5
using System . Collections . Generic ;
6
- using System . Diagnostics ;
7
6
using System . Diagnostics . CodeAnalysis ;
8
7
using System . Globalization ;
9
8
using System . Reflection . Metadata ;
10
- using System . Runtime . InteropServices ;
11
9
12
10
namespace System . Reflection . Emit
13
11
{
14
12
internal sealed class MethodBuilderImpl : MethodBuilder
15
13
{
16
- private readonly Type _returnType ;
17
- private readonly Type [ ] ? _parameterTypes ;
14
+ private Type _returnType ;
15
+ private Type [ ] ? _parameterTypes ;
18
16
private readonly ModuleBuilderImpl _module ;
19
17
private readonly string _name ;
20
18
private readonly CallingConventions _callingConventions ;
21
19
private readonly TypeBuilderImpl _declaringType ;
22
20
private MethodAttributes _attributes ;
23
21
private MethodImplAttributes _methodImplFlags ;
22
+ private GenericTypeParameterBuilderImpl [ ] ? _typeParameters ;
24
23
25
24
internal DllImportData ? _dllImportData ;
26
25
internal List < CustomAttributeWrapper > ? _customAttributes ;
@@ -49,11 +48,43 @@ internal MethodBuilderImpl(string name, MethodAttributes attributes, CallingConv
49
48
_methodImplFlags = MethodImplAttributes . IL ;
50
49
}
51
50
52
- internal BlobBuilder GetMethodSignatureBlob ( ) =>
53
- MetadataSignatureHelper . MethodSignatureEncoder ( _module , _parameterTypes , ReturnType , ! IsStatic ) ;
51
+ internal BlobBuilder GetMethodSignatureBlob ( ) => MetadataSignatureHelper . MethodSignatureEncoder ( _module ,
52
+ _parameterTypes , ReturnType , GetSignatureConvention ( _callingConventions ) , GetGenericArguments ( ) . Length , ! IsStatic ) ;
54
53
54
+ internal static SignatureCallingConvention GetSignatureConvention ( CallingConventions callingConventions )
55
+ {
56
+ // TODO: find out and handle other SignatureCallingConvention scenarios
57
+ SignatureCallingConvention convention = SignatureCallingConvention . Default ;
58
+ if ( ( callingConventions & CallingConventions . HasThis ) != 0 ||
59
+ ( callingConventions & CallingConventions . ExplicitThis ) != 0 )
60
+ {
61
+ convention |= SignatureCallingConvention . ThisCall ;
62
+ }
63
+
64
+ if ( ( callingConventions & CallingConventions . VarArgs ) != 0 )
65
+ {
66
+ convention |= SignatureCallingConvention . VarArgs ;
67
+ }
68
+
69
+ return convention ;
70
+ }
55
71
protected override bool InitLocalsCore { get => throw new NotImplementedException ( ) ; set => throw new NotImplementedException ( ) ; }
56
- protected override GenericTypeParameterBuilder [ ] DefineGenericParametersCore ( params string [ ] names ) => throw new NotImplementedException ( ) ;
72
+ protected override GenericTypeParameterBuilder [ ] DefineGenericParametersCore ( params string [ ] names )
73
+ {
74
+ if ( _typeParameters != null )
75
+ throw new InvalidOperationException ( SR . InvalidOperation_GenericParametersAlreadySet ) ;
76
+
77
+ var typeParameters = new GenericTypeParameterBuilderImpl [ names . Length ] ;
78
+ for ( int i = 0 ; i < names . Length ; i ++ )
79
+ {
80
+ string name = names [ i ] ;
81
+ ArgumentNullException . ThrowIfNull ( names , nameof ( names ) ) ;
82
+ typeParameters [ i ] = new GenericTypeParameterBuilderImpl ( name , i , this ) ;
83
+ }
84
+
85
+ return _typeParameters = typeParameters ;
86
+ }
87
+
57
88
protected override ParameterBuilder DefineParameterCore ( int position , ParameterAttributes attributes , string ? strParamName )
58
89
{
59
90
if ( position > 0 && ( _parameterTypes == null || position > _parameterTypes . Length ) )
@@ -66,6 +97,7 @@ protected override ParameterBuilder DefineParameterCore(int position, ParameterA
66
97
_parameters [ position ] = parameter ;
67
98
return parameter ;
68
99
}
100
+
69
101
protected override ILGenerator GetILGeneratorCore ( int size ) => throw new NotImplementedException ( ) ;
70
102
protected override void SetCustomAttributeCore ( ConstructorInfo con , ReadOnlySpan < byte > binaryAttribute )
71
103
{
@@ -106,15 +138,32 @@ protected override void SetImplementationFlagsCore(MethodImplAttributes attribut
106
138
_methodImplFlags = attributes ;
107
139
}
108
140
protected override void SetSignatureCore ( Type ? returnType , Type [ ] ? returnTypeRequiredCustomModifiers , Type [ ] ? returnTypeOptionalCustomModifiers , Type [ ] ? parameterTypes ,
109
- Type [ ] [ ] ? parameterTypeRequiredCustomModifiers , Type [ ] [ ] ? parameterTypeOptionalCustomModifiers ) => throw new NotImplementedException ( ) ;
141
+ Type [ ] [ ] ? parameterTypeRequiredCustomModifiers , Type [ ] [ ] ? parameterTypeOptionalCustomModifiers )
142
+ {
143
+ if ( returnType != null )
144
+ {
145
+ _returnType = returnType ;
146
+ }
147
+
148
+ if ( parameterTypes != null )
149
+ {
150
+ _parameterTypes = new Type [ parameterTypes . Length ] ;
151
+ _parameters = new ParameterBuilderImpl [ parameterTypes . Length + 1 ] ; // parameter 0 reserved for return type
152
+ for ( int i = 0 ; i < parameterTypes . Length ; i ++ )
153
+ {
154
+ ArgumentNullException . ThrowIfNull ( _parameterTypes [ i ] = parameterTypes [ i ] , nameof ( parameterTypes ) ) ;
155
+ }
156
+ }
157
+ // TODO: Add support for other parameters: returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers and parameterTypeOptionalCustomModifiers
158
+ }
110
159
public override string Name => _name ;
111
160
public override MethodAttributes Attributes => _attributes ;
112
161
public override CallingConventions CallingConvention => _callingConventions ;
113
162
public override TypeBuilder DeclaringType => _declaringType ;
114
163
public override Module Module => _module ;
115
- public override bool ContainsGenericParameters { get => throw new NotSupportedException ( SR . NotSupported_DynamicModule ) ; }
116
- public override bool IsGenericMethod { get => throw new NotImplementedException ( ) ; }
117
- public override bool IsGenericMethodDefinition { get => throw new NotImplementedException ( ) ; }
164
+ public override bool ContainsGenericParameters => throw new NotSupportedException ( ) ;
165
+ public override bool IsGenericMethod => _typeParameters != null ;
166
+ public override bool IsGenericMethodDefinition => _typeParameters != null ;
118
167
public override bool IsSecurityCritical => true ;
119
168
public override bool IsSecuritySafeCritical => false ;
120
169
public override bool IsSecurityTransparent => false ;
@@ -131,11 +180,9 @@ protected override void SetSignatureCore(Type? returnType, Type[]? returnTypeReq
131
180
132
181
public override object [ ] GetCustomAttributes ( Type attributeType , bool inherit ) => throw new NotSupportedException ( SR . NotSupported_DynamicModule ) ;
133
182
134
- public override Type [ ] GetGenericArguments ( )
135
- => throw new NotImplementedException ( ) ;
183
+ public override Type [ ] GetGenericArguments ( ) => _typeParameters ?? Type . EmptyTypes ;
136
184
137
- public override MethodInfo GetGenericMethodDefinition ( )
138
- => throw new NotImplementedException ( ) ;
185
+ public override MethodInfo GetGenericMethodDefinition ( ) => ! IsGenericMethod ? throw new InvalidOperationException ( ) : this ;
139
186
140
187
public override int GetHashCode ( )
141
188
=> throw new NotImplementedException ( ) ;
0 commit comments