IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
TypeBuilder.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2011 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.Diagnostics;
27using System.Reflection.Metadata;
28using System.Reflection.Metadata.Ecma335;
29using System.Runtime.InteropServices;
30
33
35{
36
37 internal sealed class TypeBuilder : TypeInfo, ITypeOwner
38 {
39
40 public const int UnspecifiedTypeSize = 0;
41
42 readonly ITypeOwner owner;
43 readonly int token;
44 int extends;
45 Type lazyBaseType; // (lazyBaseType == null && attribs & TypeAttributes.Interface) == 0) => BaseType == System.Object
46 readonly StringHandle typeName;
47 readonly StringHandle typeNameSpace;
48 readonly string ns;
49 readonly string name;
50 readonly List<MethodBuilder> methods = new List<MethodBuilder>();
51 readonly List<FieldBuilder> fields = new List<FieldBuilder>();
52 List<PropertyBuilder> properties;
53 List<EventBuilder> events;
54 TypeAttributes attribs;
55 GenericTypeParameterBuilder[] gtpb;
56 List<CustomAttributeBuilder> declarativeSecurity;
57 List<Type> interfaces;
58 int size;
59 short packingSize;
60 bool hasLayout;
61
68 internal TypeBuilder(ITypeOwner owner, string ns, string name)
69 {
70 this.owner = owner;
71 this.token = ModuleBuilder.TypeDefTable.AllocToken();
72 this.ns = ns;
73 this.name = name;
74 this.typeNameSpace = ns == null ? default : ModuleBuilder.GetOrAddString(ns);
75 this.typeName = ModuleBuilder.GetOrAddString(name);
76 MarkKnownType(ns, name);
77 }
78
79 public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
80 {
81 var cb = DefineConstructor(attributes, CallingConventions.Standard, Type.EmptyTypes);
82 var ilgen = cb.GetILGenerator();
83 ilgen.Emit(OpCodes.Ldarg_0);
84 ilgen.Emit(OpCodes.Call, BaseType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
85 ilgen.Emit(OpCodes.Ret);
86 return cb;
87 }
88
89 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callConv, Type[] parameterTypes)
90 {
91 return DefineConstructor(attribs, callConv, parameterTypes, null, null);
92 }
93
94 public ConstructorBuilder DefineConstructor(MethodAttributes attribs, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
95 {
96 attribs |= MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
97 var name = (attribs & MethodAttributes.Static) == 0 ? ConstructorInfo.ConstructorName : ConstructorInfo.TypeConstructorName;
98 var mb = DefineMethod(name, attribs, callingConvention, null, null, null, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
99 return new ConstructorBuilder(mb);
100 }
101
102 public ConstructorBuilder DefineTypeInitializer()
103 {
104 var mb = DefineMethod(ConstructorInfo.TypeConstructorName, MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, null, Type.EmptyTypes);
105 return new ConstructorBuilder(mb);
106 }
107
108 private MethodBuilder CreateMethodBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention)
109 {
110 ModuleBuilder.MethodDefTable.AddVirtualRecord();
111 var mb = new MethodBuilder(this, name, attributes, callingConvention);
112 methods.Add(mb);
113 return mb;
114 }
115
116 public MethodBuilder DefineMethod(string name, MethodAttributes attribs)
117 {
118 return DefineMethod(name, attribs, CallingConventions.Standard);
119 }
120
121 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, CallingConventions callingConvention)
122 {
123 return CreateMethodBuilder(name, attribs, callingConvention);
124 }
125
126 public MethodBuilder DefineMethod(string name, MethodAttributes attribs, Type returnType, Type[] parameterTypes)
127 {
128 return DefineMethod(name, attribs, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
129 }
130
131 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
132 {
133 return DefineMethod(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
134 }
135
136 public MethodBuilder DefineMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
137 {
138 var mb = CreateMethodBuilder(name, attributes, callingConvention);
139 mb.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
140 return mb;
141 }
142
143 public MethodBuilder DefinePInvokeMethod(string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
144 {
145 return DefinePInvokeMethod(name, dllName, null, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
146 }
147
148 public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
149 {
150 return DefinePInvokeMethod(name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
151 }
152
153 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)
154 {
155 var mb = DefineMethod(name, attributes | MethodAttributes.PinvokeImpl, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
156 mb.SetDllImportPseudoCustomAttribute(dllName, entryName, nativeCallConv, nativeCharSet, null, null, null, null, null);
157 return mb;
158 }
159
160 public void DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
161 {
162 var rec = new MethodImplTable.Record();
163 rec.Class = token;
164 rec.MethodBody = this.ModuleBuilder.GetMethodToken(methodInfoBody).Token;
165 rec.MethodDeclaration = this.ModuleBuilder.GetMethodTokenWinRT(methodInfoDeclaration);
166 ModuleBuilder.MethodImplTable.AddRecord(rec);
167 }
168
169 public FieldBuilder DefineField(string name, Type fieldType, FieldAttributes attribs)
170 {
171 return DefineField(name, fieldType, null, null, attribs);
172 }
173
174 public FieldBuilder DefineField(string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
175 {
176 return __DefineField(fieldName, type, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers), attributes);
177 }
178
179 public FieldBuilder __DefineField(string fieldName, Type type, CustomModifiers customModifiers, FieldAttributes attributes)
180 {
181 var fb = new FieldBuilder(this, fieldName, type, customModifiers, attributes);
182 fields.Add(fb);
183 return fb;
184 }
185
186 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
187 {
188 return DefineProperty(name, attributes, returnType, null, null, parameterTypes, null, null);
189 }
190
191 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
192 {
193 return DefineProperty(name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
194 }
195
196 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
197 {
198 return DefinePropertyImpl(name, attributes, CallingConventions.Standard, true, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
199 }
200
201 public PropertyBuilder DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
202 {
203 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, Util.NullSafeLength(parameterTypes)));
204 }
205
206 public PropertyBuilder __DefineProperty(string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
207 {
208 return DefinePropertyImpl(name, attributes, callingConvention, false, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
209 }
210
211 private PropertyBuilder DefinePropertyImpl(string name, PropertyAttributes attributes, CallingConventions callingConvention, bool patchCallingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
212 {
213 properties ??= new List<PropertyBuilder>();
214 var sig = PropertySignature.Create(callingConvention, returnType, parameterTypes, customModifiers);
215 var pb = new PropertyBuilder(this, name, attributes, sig, patchCallingConvention);
216 properties.Add(pb);
217 return pb;
218 }
219
220 public EventBuilder DefineEvent(string name, EventAttributes attributes, Type eventtype)
221 {
222 events ??= new List<EventBuilder>();
223 var eb = new EventBuilder(this, name, attributes, eventtype);
224 events.Add(eb);
225 return eb;
226 }
227
228 public TypeBuilder DefineNestedType(string name)
229 {
230 return DefineNestedType(name, TypeAttributes.Class | TypeAttributes.NestedPrivate);
231 }
232
233 public TypeBuilder DefineNestedType(string name, TypeAttributes attribs)
234 {
235 return DefineNestedType(name, attribs, null);
236 }
237
238 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
239 {
240 var tb = DefineNestedType(name, attr, parent);
241 if (interfaces != null)
242 foreach (Type iface in interfaces)
243 tb.AddInterfaceImplementation(iface);
244
245 return tb;
246 }
247
248 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent)
249 {
250 return DefineNestedType(name, attr, parent, 0);
251 }
252
253 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, int typeSize)
254 {
255 return DefineNestedType(name, attr, parent, PackingSize.Unspecified, typeSize);
256 }
257
258 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize)
259 {
260 return DefineNestedType(name, attr, parent, packSize, 0);
261 }
262
263 public TypeBuilder DefineNestedType(string name, TypeAttributes attr, Type parent, PackingSize packSize, int typeSize)
264 {
265 string ns = null;
266 var lastdot = name.LastIndexOf('.');
267 if (lastdot > 0)
268 {
269 ns = name.Substring(0, lastdot);
270 name = name.Substring(lastdot + 1);
271 }
272
273 var typeBuilder = __DefineNestedType(ns, name);
274 typeBuilder.__SetAttributes(attr);
275 typeBuilder.SetParent(parent);
276 if (packSize != PackingSize.Unspecified || typeSize != 0)
277 typeBuilder.__SetLayout((int)packSize, typeSize);
278
279 return typeBuilder;
280 }
281
282 public TypeBuilder __DefineNestedType(string ns, string name)
283 {
284 typeFlags |= TypeFlags.HasNestedTypes;
285 var typeBuilder = ModuleBuilder.DefineType(this, ns, name);
286 var rec = new NestedClassTable.Record();
287 rec.NestedClass = typeBuilder.MetadataToken;
288 rec.EnclosingClass = MetadataToken;
289 ModuleBuilder.NestedClassTable.AddRecord(rec);
290 return typeBuilder;
291 }
292
293 public void SetParent(Type parent)
294 {
295 lazyBaseType = parent;
296 }
297
298 public void AddInterfaceImplementation(Type interfaceType)
299 {
300 interfaces ??= new List<Type>();
301 interfaces.Add(interfaceType);
302 }
303
304 public void __SetInterfaceImplementationCustomAttribute(Type interfaceType, CustomAttributeBuilder cab)
305 {
306 ModuleBuilder.SetInterfaceImplementationCustomAttribute(this, interfaceType, cab);
307 }
308
309 public int Size
310 {
311 get { return size; }
312 }
313
314 public PackingSize PackingSize
315 {
316 get { return (PackingSize)packingSize; }
317 }
318
319 public override bool __GetLayout(out int packingSize, out int size)
320 {
321 packingSize = this.packingSize;
322 size = this.size;
323 return hasLayout;
324 }
325
326 public void __SetLayout(int packingSize, int size)
327 {
328 this.packingSize = (short)packingSize;
329 this.size = size;
330 hasLayout = true;
331 }
332
333 void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
334 {
335 var val = customBuilder.GetConstructorArgument(0);
336 var layout = val is short v ? (LayoutKind)v : (LayoutKind)val;
337 packingSize = (short)((int?)customBuilder.GetFieldValue("Pack") ?? 0);
338 size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
339
340 attribs &= ~TypeAttributes.LayoutMask;
341 switch (layout)
342 {
343 case LayoutKind.Auto:
344 attribs |= TypeAttributes.AutoLayout;
345 break;
346 case LayoutKind.Explicit:
347 attribs |= TypeAttributes.ExplicitLayout;
348 break;
349 case LayoutKind.Sequential:
350 attribs |= TypeAttributes.SequentialLayout;
351 break;
352 }
353
354 attribs &= ~TypeAttributes.StringFormatMask;
355
356 switch (customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None)
357 {
358 case CharSet.None:
359 case CharSet.Ansi:
360 attribs |= TypeAttributes.AnsiClass;
361 break;
362 case CharSet.Auto:
363 attribs |= TypeAttributes.AutoClass;
364 break;
365 case CharSet.Unicode:
366 attribs |= TypeAttributes.UnicodeClass;
367 break;
368 }
369
370 hasLayout = packingSize != 0 || size != 0;
371 }
372
373 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
374 {
375 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
376 }
377
378 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
379 {
380 switch (customBuilder.KnownCA)
381 {
382 case KnownCA.StructLayoutAttribute:
383 SetStructLayoutPseudoCustomAttribute(customBuilder.DecodeBlob(this.Assembly));
384 break;
385 case KnownCA.SerializableAttribute:
386 attribs |= TypeAttributes.Serializable;
387 break;
388 case KnownCA.ComImportAttribute:
389 attribs |= TypeAttributes.Import;
390 break;
391 case KnownCA.SpecialNameAttribute:
392 attribs |= TypeAttributes.SpecialName;
393 break;
394 case KnownCA.SuppressUnmanagedCodeSecurityAttribute:
395 attribs |= TypeAttributes.HasSecurity;
396 goto default;
397 default:
398 ModuleBuilder.SetCustomAttribute(token, customBuilder);
399 break;
400 }
401 }
402
403 public void __AddDeclarativeSecurity(CustomAttributeBuilder customBuilder)
404 {
405 attribs |= TypeAttributes.HasSecurity;
406 declarativeSecurity ??= new List<CustomAttributeBuilder>();
407 declarativeSecurity.Add(customBuilder);
408 }
409
410 public void AddDeclarativeSecurity(System.Security.Permissions.SecurityAction securityAction, System.Security.PermissionSet permissionSet)
411 {
412 ModuleBuilder.AddDeclarativeSecurity(token, securityAction, permissionSet);
413 attribs |= TypeAttributes.HasSecurity;
414 }
415
416 public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] names)
417 {
418 typeFlags |= TypeFlags.IsGenericTypeDefinition;
419 gtpb = new GenericTypeParameterBuilder[names.Length];
420 for (int i = 0; i < names.Length; i++)
421 gtpb[i] = new GenericTypeParameterBuilder(names[i], this, i);
422
423 return (GenericTypeParameterBuilder[])gtpb.Clone();
424 }
425
426 public override Type[] GetGenericArguments()
427 {
428 return Util.Copy(gtpb);
429 }
430
431 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
432 {
433 return gtpb == null ? Array.Empty<CustomModifiers>() : new CustomModifiers[gtpb.Length];
434 }
435
436 internal override Type GetGenericTypeArgument(int index)
437 {
438 return gtpb[index];
439 }
440
441 public override bool ContainsGenericParameters
442 {
443 get { return gtpb != null; }
444 }
445
446 public override Type GetGenericTypeDefinition()
447 {
448 return this;
449 }
450
451 public TypeInfo CreateTypeInfo()
452 {
453 // .NET allows multiple invocations (subsequent invocations return the same baked type)
454 if ((typeFlags & TypeFlags.Baked) != 0)
455 throw new NotImplementedException();
456
457 typeFlags |= TypeFlags.Baked;
458 if (hasLayout)
459 {
460 var rec = new ClassLayoutTable.Record();
461 rec.PackingSize = packingSize;
462 rec.ClassSize = size;
463 rec.Parent = token;
464 ModuleBuilder.ClassLayoutTable.AddRecord(rec);
465 }
466
467 var hasConstructor = false;
468 foreach (var mb in methods)
469 {
470 hasConstructor |= mb.IsSpecialName && mb.Name == ConstructorInfo.ConstructorName;
471 mb.Bake();
472 }
473
474 if (!hasConstructor && !IsModulePseudoType && !IsInterface && !IsValueType && !(IsAbstract && IsSealed) && Universe.AutomaticallyProvideDefaultConstructor)
475 ((MethodBuilder)DefineDefaultConstructor(MethodAttributes.Public).GetMethodInfo()).Bake();
476
477 if (declarativeSecurity != null)
478 ModuleBuilder.AddDeclarativeSecurity(token, declarativeSecurity);
479
480 if (!IsModulePseudoType)
481 {
482 var baseType = BaseType;
483 if (baseType != null)
484 extends = ModuleBuilder.GetTypeToken(baseType).Token;
485 }
486
487 if (interfaces != null)
488 {
489 foreach (var interfaceType in interfaces)
490 {
491 var rec = new InterfaceImplTable.Record();
492 rec.Class = token;
493 rec.Interface = ModuleBuilder.GetTypeToken(interfaceType).Token;
494 ModuleBuilder.InterfaceImplTable.AddRecord(rec);
495 }
496 }
497
498 return new BakedType(this);
499 }
500
501 public Type CreateType()
502 {
503 return CreateTypeInfo();
504 }
505
506 internal void PopulatePropertyAndEventTables()
507 {
508 if (properties != null)
509 {
510 var rec = new PropertyMapTable.Record();
511 rec.Parent = token;
512 rec.PropertyList = MetadataTokens.GetToken(MetadataTokens.PropertyDefinitionHandle(ModuleBuilder.PropertyTable.RowCount + 1));
513 ModuleBuilder.PropertyMapTable.AddRecord(rec);
514 foreach (var pb in properties)
515 pb.Bake();
516 }
517
518 if (events != null)
519 {
520 var rec = new EventMapTable.Record();
521 rec.Parent = token;
522 rec.EventList = MetadataTokens.GetToken(MetadataTokens.EventDefinitionHandle(ModuleBuilder.EventTable.RowCount + 1));
523 ModuleBuilder.EventMapTable.AddRecord(rec);
524 foreach (var eb in events)
525 eb.Bake();
526 }
527 }
528
529 public override Type BaseType
530 {
531 get
532 {
533 if (lazyBaseType == null && !IsInterface)
534 {
535 var obj = Module.Universe.System_Object;
536 if (this != obj)
537 lazyBaseType = obj;
538 }
539
540 return lazyBaseType;
541 }
542 }
543
544 public override string FullName
545 {
546 get
547 {
548 if (IsNested)
549 return DeclaringType.FullName + "+" + TypeNameParser.Escape(name);
550
551 if (ns == null)
552 return TypeNameParser.Escape(name);
553 else
554 return TypeNameParser.Escape(ns) + "." + TypeNameParser.Escape(name);
555 }
556 }
557
558 internal override TypeName TypeName
559 {
560 get { return new TypeName(ns, name); }
561 }
562
563 public override string Name
564 {
565 // FXBUG for a TypeBuilder the name is not escaped
566 get { return name; }
567 }
568
569 public override string Namespace
570 {
571 get
572 {
573 // for some reason, TypeBuilder doesn't return null (and mcs depends on this)
574 // note also that we don't return the declaring type namespace for nested types
575 return ns ?? "";
576 }
577 }
578
579 public override TypeAttributes Attributes
580 {
581 get { return attribs; }
582 }
583
584 public void __SetAttributes(TypeAttributes attributes)
585 {
586 attribs = attributes;
587 }
588
589 public override Type[] __GetDeclaredInterfaces()
590 {
591 return Util.ToArray(interfaces, Type.EmptyTypes);
592 }
593
594 public override MethodBase[] __GetDeclaredMethods()
595 {
596 var methods = new MethodBase[this.methods.Count];
597 for (int i = 0; i < methods.Length; i++)
598 {
599 var mb = this.methods[i];
600 methods[i] = mb.IsConstructor ? new ConstructorInfoImpl(mb) : mb;
601 }
602
603 return methods;
604 }
605
606 public override Type DeclaringType
607 {
608 get { return owner as TypeBuilder; }
609 }
610
611 public override bool IsGenericType
612 {
613 get { return IsGenericTypeDefinition; }
614 }
615
616 public override bool IsGenericTypeDefinition
617 {
618 get { return (typeFlags & TypeFlags.IsGenericTypeDefinition) != 0; }
619 }
620
621 public override int MetadataToken
622 {
623 get { return token; }
624 }
625
626 public FieldBuilder DefineUninitializedData(string name, int size, FieldAttributes attributes)
627 {
628 return DefineInitializedData(name, new byte[size], attributes);
629 }
630
631 public FieldBuilder DefineInitializedData(string name, byte[] data, FieldAttributes attributes)
632 {
633 throw new NotImplementedException();
634
635 //var fieldType = ModuleBuilder.GetType("$ArrayType$" + data.Length);
636 //if (fieldType == null)
637 //{
638 // var typeBuilder = ModuleBuilder.DefineType("$ArrayType$" + data.Length, TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.ExplicitLayout, Module.Universe.System_ValueType, PackingSize.Size1, data.Length);
639 // typeBuilder.CreateType();
640 // fieldType = typeBuilder;
641 //}
642
643 //var fieldBuilder = DefineField(name, fieldType, attributes | FieldAttributes.Static);
644 //fieldBuilder.__SetDataAndRVA(data);
645 //return fieldBuilder;
646 }
647
648 public static MethodInfo GetMethod(Type type, MethodInfo method)
649 {
650 return new GenericMethodInstance(type, method, null);
651 }
652
653 public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
654 {
655 return new ConstructorInfoImpl(GetMethod(type, constructor.GetMethodInfo()));
656 }
657
658 public static FieldInfo GetField(Type type, FieldInfo field)
659 {
660 return new GenericFieldInstance(type, field);
661 }
662
663 public override Module Module
664 {
665 get { return owner.ModuleBuilder; }
666 }
667
668 public TypeToken TypeToken
669 {
670 get { return new TypeToken(token); }
671 }
672
673 internal void WriteTypeDefRecord(ref int fieldList, ref int methodList)
674 {
675 var h = ModuleBuilder.Metadata.AddTypeDefinition(
676 (System.Reflection.TypeAttributes)attribs,
677 typeNameSpace,
678 typeName,
679 MetadataTokens.EntityHandle(extends),
680 MetadataTokens.FieldDefinitionHandle(fieldList),
681 MetadataTokens.MethodDefinitionHandle(methodList));
682
683 Debug.Assert(h == MetadataTokens.TypeDefinitionHandle(token));
684
685 // increment next expected method and field row numbers
686 fieldList += fields.Count;
687 methodList += methods.Count;
688 }
689
690 internal void WriteMethodDefRecords(ref int paramList)
691 {
692 foreach (var mb in methods)
693 mb.WriteMetadata(ref paramList);
694 }
695
696 internal void ResolveMethodAndFieldTokens(ref int methodToken, ref int fieldToken, ref int parameterToken)
697 {
698 foreach (var method in methods)
699 method.FixupToken(methodToken++, ref parameterToken);
700 foreach (var field in fields)
701 field.FixupToken(fieldToken++);
702 }
703
704 internal void WriteParamRecords()
705 {
706 foreach (var mb in methods)
707 mb.WriteParamRecords();
708 }
709
710 internal void WriteFieldRecords()
711 {
712 foreach (var fb in fields)
713 fb.WriteFieldRecords();
714 }
715
716 internal ModuleBuilder ModuleBuilder
717 {
718 get { return owner.ModuleBuilder; }
719 }
720
721 ModuleBuilder ITypeOwner.ModuleBuilder
722 {
723 get { return owner.ModuleBuilder; }
724 }
725
726 internal override int GetModuleBuilderToken()
727 {
728 return token;
729 }
730
731 internal bool HasNestedTypes
732 {
733 get { return (typeFlags & TypeFlags.HasNestedTypes) != 0; }
734 }
735
741 internal MethodBase LookupMethod(int token)
742 {
743 foreach (var method in methods)
744 if (method.MetadataToken == token)
745 return method;
746
747 return null;
748 }
749
750 public bool IsCreated()
751 {
752 return (typeFlags & TypeFlags.Baked) != 0;
753 }
754
755 internal override void CheckBaked()
756 {
757 if ((typeFlags & TypeFlags.Baked) == 0)
758 throw new NotSupportedException();
759 }
760
761 public override Type[] __GetDeclaredTypes()
762 {
763 if (HasNestedTypes)
764 {
765 var types = new List<Type>();
766 var classes = ModuleBuilder.NestedClassTable.GetNestedClasses(token);
767 foreach (var nestedClass in classes)
768 types.Add(ModuleBuilder.ResolveType(nestedClass));
769
770 return types.ToArray();
771 }
772 else
773 {
774 return Type.EmptyTypes;
775 }
776 }
777
778 public override FieldInfo[] __GetDeclaredFields()
779 {
780 return Util.ToArray(fields, Array.Empty<FieldInfo>());
781 }
782
783 public override EventInfo[] __GetDeclaredEvents()
784 {
785 return Util.ToArray(events, Array.Empty<EventInfo>());
786 }
787
788 public override PropertyInfo[] __GetDeclaredProperties()
789 {
790 return Util.ToArray(properties, Array.Empty<PropertyInfo>());
791 }
792
793 internal override bool IsModulePseudoType
794 {
795 get { return token == 0x02000001; }
796 }
797
798 internal override bool IsBaked
799 {
800 get { return IsCreated(); }
801 }
802
803 protected override bool IsValueTypeImpl
804 {
805 get
806 {
807 var baseType = this.BaseType;
808 if (baseType != null && baseType.IsEnumOrValueType && !this.IsEnumOrValueType)
809 {
810 if (IsCreated())
811 typeFlags |= TypeFlags.ValueType;
812
813 return true;
814 }
815 else
816 {
817 if (IsCreated())
818 typeFlags |= TypeFlags.NotValueType;
819
820 return false;
821 }
822 }
823 }
824
825 }
826
827}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.EventInfo EventInfo
IKVM.Reflection.FieldInfo FieldInfo
IKVM.Reflection.PropertyInfo PropertyInfo
IKVM.Reflection.MethodInfo MethodInfo
IKVM.Reflection.MethodBase MethodBase
global::java.lang.invoke.LambdaForm.Name Name
System.Runtime.InteropServices.CallingConvention CallingConvention
Definition Signature.cs:35
KnownCA
These are the pseudo-custom attributes that are recognized by name by the runtime (i....
Definition KnownCA.cs:35