5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
31 public sealed class TypeDefinition : TypeReference, IMemberDefinition, IHasSecurity {
33 TypeAttributes m_attributes;
34 TypeReference m_baseType;
40 InterfaceCollection m_interfaces;
41 NestedTypeCollection m_nestedTypes;
42 MethodDefinitionCollection m_methods;
43 ConstructorCollection m_ctors;
44 FieldDefinitionCollection m_fields;
45 EventDefinitionCollection m_events;
46 PropertyDefinitionCollection m_properties;
47 SecurityDeclarationCollection m_secDecls;
49 public TypeAttributes Attributes {
50 get { return m_attributes; }
51 set { m_attributes = value; }
54 public TypeReference BaseType {
55 get { return m_baseType; }
56 set { m_baseType = value; }
59 public bool HasLayoutInfo {
60 get { return m_hasInfo; }
63 public ushort PackingSize {
64 get { return m_packingSize; }
67 m_packingSize = value;
71 public uint ClassSize {
72 get { return m_classSize; }
79 public bool HasInterfaces {
80 get { return (m_interfaces == null) ? false : (m_interfaces.Count > 0); }
83 public InterfaceCollection Interfaces {
85 if (m_interfaces == null)
86 m_interfaces = new InterfaceCollection (this);
92 public bool HasNestedTypes {
93 get { return (m_nestedTypes == null) ? false : (m_nestedTypes.Count > 0); }
96 public NestedTypeCollection NestedTypes {
98 if (m_nestedTypes == null)
99 m_nestedTypes = new NestedTypeCollection (this);
101 return m_nestedTypes;
105 public bool HasMethods {
106 get { return (m_methods == null) ? false : (m_methods.Count > 0); }
109 public MethodDefinitionCollection Methods {
111 if (m_methods == null)
112 m_methods = new MethodDefinitionCollection (this);
118 public bool HasConstructors {
119 get { return (m_ctors == null) ? false : (m_ctors.Count > 0); }
122 public ConstructorCollection Constructors {
125 m_ctors = new ConstructorCollection (this);
131 public bool HasFields {
132 get { return (m_fields == null) ? false : (m_fields.Count > 0); }
135 public FieldDefinitionCollection Fields {
137 if (m_fields == null)
138 m_fields = new FieldDefinitionCollection (this);
144 public bool HasEvents {
145 get { return (m_events == null) ? false : (m_events.Count > 0); }
148 public EventDefinitionCollection Events {
150 if (m_events == null)
151 m_events = new EventDefinitionCollection (this);
157 public bool HasProperties {
158 get { return (m_properties == null) ? false : (m_properties.Count > 0); }
161 public PropertyDefinitionCollection Properties {
163 if (m_properties == null)
164 m_properties = new PropertyDefinitionCollection (this);
170 public bool HasSecurityDeclarations {
171 get { return (m_secDecls == null) ? false : (m_secDecls.Count > 0); }
174 public SecurityDeclarationCollection SecurityDeclarations {
176 if (m_secDecls == null)
177 m_secDecls = new SecurityDeclarationCollection (this);
183 #region TypeAttributes
185 public bool IsNotPublic {
186 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic; }
189 m_attributes &= ~TypeAttributes.VisibilityMask;
190 m_attributes |= TypeAttributes.NotPublic;
192 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NotPublic);
196 public bool IsPublic {
197 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public; }
200 m_attributes &= ~TypeAttributes.VisibilityMask;
201 m_attributes |= TypeAttributes.Public;
203 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.Public);
207 public bool IsNestedPublic {
208 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic; }
211 m_attributes &= ~TypeAttributes.VisibilityMask;
212 m_attributes |= TypeAttributes.NestedPublic;
214 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPublic);
218 public bool IsNestedPrivate {
219 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate; }
222 m_attributes &= ~TypeAttributes.VisibilityMask;
223 m_attributes |= TypeAttributes.NestedPrivate;
225 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPrivate);
229 public bool IsNestedFamily {
230 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily; }
233 m_attributes &= ~TypeAttributes.VisibilityMask;
234 m_attributes |= TypeAttributes.NestedFamily;
236 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamily);
240 public bool IsNestedAssembly {
241 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly; }
244 m_attributes &= ~TypeAttributes.VisibilityMask;
245 m_attributes |= TypeAttributes.NestedAssembly;
247 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedAssembly);
251 public bool IsNestedFamilyAndAssembly {
252 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem; }
255 m_attributes &= ~TypeAttributes.VisibilityMask;
256 m_attributes |= TypeAttributes.NestedFamANDAssem;
258 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamANDAssem);
262 public bool IsNestedFamilyOrAssembly {
263 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem; }
266 m_attributes &= ~TypeAttributes.VisibilityMask;
267 m_attributes |= TypeAttributes.NestedFamORAssem;
269 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamORAssem);
273 public bool IsAutoLayout {
274 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
277 m_attributes &= ~TypeAttributes.LayoutMask;
278 m_attributes |= TypeAttributes.AutoLayout;
280 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.AutoLayout);
284 public bool IsSequentialLayout {
285 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
288 m_attributes &= ~TypeAttributes.LayoutMask;
289 m_attributes |= TypeAttributes.SequentialLayout;
291 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.SequentialLayout);
295 public bool IsExplicitLayout {
296 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
299 m_attributes &= ~TypeAttributes.LayoutMask;
300 m_attributes |= TypeAttributes.ExplicitLayout;
302 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.ExplicitLayout);
306 public bool IsClass {
307 get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Class; }
310 m_attributes &= ~TypeAttributes.ClassSemanticMask;
311 m_attributes |= TypeAttributes.Class;
313 m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Class);
317 public bool IsInterface {
318 get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Interface; }
321 m_attributes &= ~TypeAttributes.ClassSemanticMask;
322 m_attributes |= TypeAttributes.Interface;
324 m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Interface);
328 public bool IsAbstract {
329 get { return (m_attributes & TypeAttributes.Abstract) != 0; }
332 m_attributes |= TypeAttributes.Abstract;
334 m_attributes &= ~TypeAttributes.Abstract;
338 public bool IsSealed {
339 get { return (m_attributes & TypeAttributes.Sealed) != 0; }
342 m_attributes |= TypeAttributes.Sealed;
344 m_attributes &= ~TypeAttributes.Sealed;
348 public bool IsSpecialName {
349 get { return (m_attributes & TypeAttributes.SpecialName) != 0; }
352 m_attributes |= TypeAttributes.SpecialName;
354 m_attributes &= ~TypeAttributes.SpecialName;
358 public bool IsImport {
359 get { return (m_attributes & TypeAttributes.Import) != 0; }
362 m_attributes |= TypeAttributes.Import;
364 m_attributes &= ~TypeAttributes.Import;
368 public bool IsSerializable {
369 get { return (m_attributes & TypeAttributes.Serializable) != 0; }
372 m_attributes |= TypeAttributes.Serializable;
374 m_attributes &= ~TypeAttributes.Serializable;
378 public bool IsAnsiClass {
379 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
382 m_attributes &= ~TypeAttributes.StringFormatMask;
383 m_attributes |= TypeAttributes.AnsiClass;
385 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AnsiClass);
389 public bool IsUnicodeClass {
390 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
393 m_attributes &= ~TypeAttributes.StringFormatMask;
394 m_attributes |= TypeAttributes.UnicodeClass;
396 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.UnicodeClass);
400 public bool IsAutoClass {
401 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
404 m_attributes &= ~TypeAttributes.StringFormatMask;
405 m_attributes |= TypeAttributes.AutoClass;
407 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AutoClass);
411 public bool IsBeforeFieldInit {
412 get { return (m_attributes & TypeAttributes.BeforeFieldInit) != 0; }
415 m_attributes |= TypeAttributes.BeforeFieldInit;
417 m_attributes &= ~TypeAttributes.BeforeFieldInit;
421 public bool IsRuntimeSpecialName {
422 get { return (m_attributes & TypeAttributes.RTSpecialName) != 0; }
425 m_attributes |= TypeAttributes.RTSpecialName;
427 m_attributes &= ~TypeAttributes.RTSpecialName;
431 public bool HasSecurity {
432 get { return (m_attributes & TypeAttributes.HasSecurity) != 0; }
435 m_attributes |= TypeAttributes.HasSecurity;
437 m_attributes &= ~TypeAttributes.HasSecurity;
444 get { return m_baseType != null && m_baseType.FullName == Constants.Enum; }
447 public override bool IsValueType {
449 return m_baseType != null && ((m_baseType.FullName == Constants.Enum) ||
450 (m_baseType.FullName == Constants.ValueType && this.FullName != Constants.Enum));
454 public new TypeDefinition DeclaringType {
455 get { return (TypeDefinition) base.DeclaringType; }
456 set { base.DeclaringType = value; }
459 internal TypeDefinition (string name, string ns, TypeAttributes attrs) :
463 m_attributes = attrs;
466 public TypeDefinition (string name, string ns,
467 TypeAttributes attributes, TypeReference baseType) :
468 this (name, ns, attributes)
470 this.BaseType = baseType;
473 public override TypeDefinition Resolve ()
478 public TypeDefinition Clone ()
480 return Clone (this, new ImportContext (NullReferenceImporter.Instance, this));
483 internal static TypeDefinition Clone (TypeDefinition type, ImportContext context)
485 TypeDefinition nt = new TypeDefinition (
490 TypeReference contextType = context.GenericContext.Type;
492 context.GenericContext.Type = nt;
494 GenericParameter.CloneInto (type, nt, context);
496 if (type.BaseType != null)
497 nt.BaseType = context.Import (type.BaseType);
499 if (type.HasLayoutInfo) {
500 nt.ClassSize = type.ClassSize;
501 nt.PackingSize = type.PackingSize;
504 if (type.HasFields) {
505 foreach (FieldDefinition field in type.Fields)
506 nt.Fields.Add (FieldDefinition.Clone (field, context));
508 if (type.HasConstructors) {
509 foreach (MethodDefinition ctor in type.Constructors)
510 nt.Constructors.Add (MethodDefinition.Clone (ctor, context));
512 if (type.HasMethods) {
513 foreach (MethodDefinition meth in type.Methods)
514 nt.Methods.Add (MethodDefinition.Clone (meth, context));
516 if (type.HasEvents) {
517 foreach (EventDefinition evt in type.Events)
518 nt.Events.Add (EventDefinition.Clone (evt, context));
520 if (type.HasProperties) {
521 foreach (PropertyDefinition prop in type.Properties)
522 nt.Properties.Add (PropertyDefinition.Clone (prop, context));
524 if (type.HasInterfaces) {
525 foreach (TypeReference intf in type.Interfaces)
526 nt.Interfaces.Add (context.Import (intf));
528 if (type.HasNestedTypes) {
529 foreach (TypeDefinition nested in type.NestedTypes)
530 nt.NestedTypes.Add (Clone (nested, context));
532 if (type.HasCustomAttributes) {
533 foreach (CustomAttribute ca in type.CustomAttributes)
534 nt.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
536 if (type.HasSecurityDeclarations) {
537 foreach (SecurityDeclaration dec in type.SecurityDeclarations)
538 nt.SecurityDeclarations.Add (SecurityDeclaration.Clone (dec));
541 context.GenericContext.Type = contextType;
546 public override void Accept (IReflectionVisitor visitor)
548 visitor.VisitTypeDefinition (this);
550 this.GenericParameters.Accept (visitor);
551 this.Interfaces.Accept (visitor);
552 this.Constructors.Accept (visitor);
553 this.Methods.Accept (visitor);
554 this.Fields.Accept (visitor);
555 this.Properties.Accept (visitor);
556 this.Events.Accept (visitor);
557 this.NestedTypes.Accept (visitor);
558 this.CustomAttributes.Accept (visitor);
559 this.SecurityDeclarations.Accept (visitor);