/
TypeBuilder.cs
324 lines (242 loc) · 17.4 KB
/
TypeBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace System.Reflection.Emit
{
public abstract partial class TypeBuilder : TypeInfo
{
protected TypeBuilder()
{
}
public const int UnspecifiedTypeSize = 0;
public PackingSize PackingSize
=> PackingSizeCore;
protected abstract PackingSize PackingSizeCore { get; }
public int Size
=> SizeCore;
protected abstract int SizeCore { get; }
public void AddInterfaceImplementation([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType)
{
ArgumentNullException.ThrowIfNull(interfaceType);
AddInterfaceImplementationCore(interfaceType);
}
protected abstract void AddInterfaceImplementationCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType);
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public Type CreateType()
=> CreateTypeInfo();
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public TypeInfo CreateTypeInfo()
=> CreateTypeInfoCore();
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
protected abstract TypeInfo CreateTypeInfoCore();
public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention, Type[]? parameterTypes)
=> DefineConstructor(attributes, callingConvention, parameterTypes, null, null);
public ConstructorBuilder DefineConstructor(MethodAttributes attributes, CallingConventions callingConvention,
Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers)
=> DefineConstructorCore(attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
protected abstract ConstructorBuilder DefineConstructorCore(MethodAttributes attributes, CallingConventions callingConvention,
Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
=> DefineDefaultConstructorCore(attributes);
protected abstract ConstructorBuilder DefineDefaultConstructorCore(MethodAttributes attributes);
public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
{
ArgumentException.ThrowIfNullOrEmpty(name);
return DefineEventCore(name, attributes, eventtype);
}
protected abstract EventBuilder DefineEventCore(string name, EventAttributes attributes, Type eventtype);
public FieldBuilder DefineField(string fieldName, Type type, FieldAttributes attributes)
=> DefineField(fieldName, type, null, null, attributes);
public FieldBuilder DefineField(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers,
FieldAttributes attributes)
{
ArgumentException.ThrowIfNullOrEmpty(fieldName);
ArgumentNullException.ThrowIfNull(type);
return DefineFieldCore(fieldName, type, requiredCustomModifiers, optionalCustomModifiers, attributes);
}
protected abstract FieldBuilder DefineFieldCore(string fieldName, Type type, Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers,
FieldAttributes attributes);
public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
{
ArgumentNullException.ThrowIfNull(names);
if (names.Length == 0)
throw new ArgumentException(SR.Arg_EmptyArray, nameof(names));
return DefineGenericParametersCore(names);
}
protected abstract GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names);
public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
{
ArgumentNullException.ThrowIfNull(data);
return DefineInitializedDataCore(name, data, attributes);
}
protected abstract FieldBuilder DefineInitializedDataCore(string name, byte[] data, FieldAttributes attributes);
public MethodBuilder DefineMethod(string name, MethodAttributes attributes)
=> DefineMethod(name, attributes, CallingConventions.Standard, null, null);
public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention)
=> DefineMethod(name, attributes, callingConvention, null, null);
public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention,
Type? returnType, Type[]? parameterTypes)
=> DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
public MethodBuilder DefineMethod(string name, MethodAttributes attributes, Type? returnType, Type[]? parameterTypes)
=> DefineMethod(name, attributes, CallingConventions.Standard, returnType, parameterTypes);
public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention,
Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
{
ArgumentException.ThrowIfNullOrEmpty(name);
if (parameterTypes != null)
{
if (parameterTypeOptionalCustomModifiers != null && parameterTypeOptionalCustomModifiers.Length != parameterTypes.Length)
throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeOptionalCustomModifiers), nameof(parameterTypes)));
if (parameterTypeRequiredCustomModifiers != null && parameterTypeRequiredCustomModifiers.Length != parameterTypes.Length)
throw new ArgumentException(SR.Format(SR.Argument_MismatchedArrays, nameof(parameterTypeRequiredCustomModifiers), nameof(parameterTypes)));
}
return DefineMethodCore(name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
}
protected abstract MethodBuilder DefineMethodCore(string name, MethodAttributes attributes, CallingConventions callingConvention,
Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
{
ArgumentNullException.ThrowIfNull(methodInfoBody);
ArgumentNullException.ThrowIfNull(methodInfoDeclaration);
DefineMethodOverrideCore(methodInfoBody, methodInfoDeclaration);
}
protected abstract void DefineMethodOverrideCore(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration);
public TypeBuilder DefineNestedType(string name)
=> DefineNestedType(name, TypeAttributes.NestedPrivate, null, null);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr)
=> DefineNestedType(name, attr, null, null);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent)
=> DefineNestedType(name, attr, parent, null);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces)
{
ArgumentException.ThrowIfNullOrEmpty(name);
return DefineNestedTypeCore(name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
}
protected abstract TypeBuilder DefineNestedTypeCore(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packSize, int typeSize);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, int typeSize)
=> DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize)
=> DefineNestedType(name, attr, parent, packSize, UnspecifiedTypeSize);
public TypeBuilder DefineNestedType(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, PackingSize packSize, int typeSize)
{
ArgumentException.ThrowIfNullOrEmpty(name);
return DefineNestedTypeCore(name, attr, parent, null, packSize, typeSize);
}
[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes,
CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes,
CallingConvention nativeCallConv, CharSet nativeCharSet)
=> DefinePInvokeMethod(name, dllName, name, attributes, callingConvention, returnType, null, null,
parameterTypes, null, null, nativeCallConv, nativeCharSet);
[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes,
CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes,
CallingConvention nativeCallConv, CharSet nativeCharSet)
=> DefinePInvokeMethod(
name, dllName, entryName, attributes, callingConvention, returnType, null, null,
parameterTypes, null, null, nativeCallConv, nativeCharSet);
[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes,
CallingConventions callingConvention,
Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers,
CallingConvention nativeCallConv, CharSet nativeCharSet)
{
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentException.ThrowIfNullOrEmpty(dllName);
ArgumentException.ThrowIfNullOrEmpty(entryName);
return DefinePInvokeMethodCore(name, dllName, entryName, attributes, callingConvention,
returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers,
nativeCallConv, nativeCharSet);
}
[RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
protected abstract MethodBuilder DefinePInvokeMethodCore(string name, string dllName, string entryName, MethodAttributes attributes,
CallingConventions callingConvention,
Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers,
CallingConvention nativeCallConv, CharSet nativeCharSet);
public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[]? parameterTypes)
=> DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes,
CallingConventions callingConvention, Type returnType, Type[]? parameterTypes)
=> DefineProperty(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes,
Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
=> DefineProperty(name, attributes, default,
returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention,
Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
{
ArgumentException.ThrowIfNullOrEmpty(name);
return DefinePropertyCore(name, attributes, callingConvention,
returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
}
protected abstract PropertyBuilder DefinePropertyCore(string name, PropertyAttributes attributes, CallingConventions callingConvention,
Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
public ConstructorBuilder DefineTypeInitializer()
=> DefineTypeInitializerCore();
protected abstract ConstructorBuilder DefineTypeInitializerCore();
public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
=> DefineUninitializedDataCore(name, size, attributes);
protected abstract FieldBuilder DefineUninitializedDataCore(string name, int size, FieldAttributes attributes);
public bool IsCreated()
=> IsCreatedCore();
protected abstract bool IsCreatedCore();
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
{
ArgumentNullException.ThrowIfNull(con);
ArgumentNullException.ThrowIfNull(binaryAttribute);
SetCustomAttributeCore(con, binaryAttribute);
}
protected abstract void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute);
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
{
ArgumentNullException.ThrowIfNull(customBuilder);
SetCustomAttributeCore(customBuilder.Ctor, customBuilder.Data);
}
public void SetParent([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent)
=> SetParentCore(parent);
protected abstract void SetParentCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent);
public override Type MakePointerType()
{
return SymbolType.FormCompoundType("*", this, 0)!;
}
public override Type MakeByRefType()
{
return SymbolType.FormCompoundType("&", this, 0)!;
}
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
string s = GetRankString(rank);
return SymbolType.FormCompoundType(s, this, 0)!;
}
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] typeArguments)
{
return TypeBuilderInstantiation.MakeGenericType(this, typeArguments);
}
}
}