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 InterfaceCollection Interfaces {
81 if (m_interfaces == null)
82 m_interfaces = new InterfaceCollection (this);
88 public NestedTypeCollection NestedTypes {
90 if (m_nestedTypes == null)
91 m_nestedTypes = new NestedTypeCollection (this);
97 public MethodDefinitionCollection Methods {
99 if (m_methods == null)
100 m_methods = new MethodDefinitionCollection (this);
106 public ConstructorCollection Constructors {
109 m_ctors = new ConstructorCollection (this);
115 public FieldDefinitionCollection Fields {
117 if (m_fields == null)
118 m_fields = new FieldDefinitionCollection (this);
124 public EventDefinitionCollection Events {
126 if (m_events == null)
127 m_events = new EventDefinitionCollection (this);
133 public PropertyDefinitionCollection Properties {
135 if (m_properties == null)
136 m_properties = new PropertyDefinitionCollection (this);
142 public SecurityDeclarationCollection SecurityDeclarations {
144 if (m_secDecls == null)
145 m_secDecls = new SecurityDeclarationCollection (this);
151 #region TypeAttributes
153 public bool IsNotPublic {
154 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic; }
157 m_attributes &= ~TypeAttributes.VisibilityMask;
158 m_attributes |= TypeAttributes.NotPublic;
160 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NotPublic);
164 public bool IsPublic {
165 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public; }
168 m_attributes &= ~TypeAttributes.VisibilityMask;
169 m_attributes |= TypeAttributes.Public;
171 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.Public);
175 public bool IsNestedPublic {
176 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic; }
179 m_attributes &= ~TypeAttributes.VisibilityMask;
180 m_attributes |= TypeAttributes.NestedPublic;
182 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPublic);
186 public bool IsNestedPrivate {
187 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate; }
190 m_attributes &= ~TypeAttributes.VisibilityMask;
191 m_attributes |= TypeAttributes.NestedPrivate;
193 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedPrivate);
197 public bool IsNestedFamily {
198 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily; }
201 m_attributes &= ~TypeAttributes.VisibilityMask;
202 m_attributes |= TypeAttributes.NestedFamily;
204 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamily);
208 public bool IsNestedAssembly {
209 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly; }
212 m_attributes &= ~TypeAttributes.VisibilityMask;
213 m_attributes |= TypeAttributes.NestedAssembly;
215 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedAssembly);
219 public bool IsNestedFamilyAndAssembly {
220 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem; }
223 m_attributes &= ~TypeAttributes.VisibilityMask;
224 m_attributes |= TypeAttributes.NestedFamANDAssem;
226 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamANDAssem);
230 public bool IsNestedFamilyOrAssembly {
231 get { return (m_attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem; }
234 m_attributes &= ~TypeAttributes.VisibilityMask;
235 m_attributes |= TypeAttributes.NestedFamORAssem;
237 m_attributes &= ~(TypeAttributes.VisibilityMask & TypeAttributes.NestedFamORAssem);
241 public bool IsAutoLayout {
242 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
245 m_attributes &= ~TypeAttributes.LayoutMask;
246 m_attributes |= TypeAttributes.AutoLayout;
248 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.AutoLayout);
252 public bool IsSequentialLayout {
253 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
256 m_attributes &= ~TypeAttributes.LayoutMask;
257 m_attributes |= TypeAttributes.SequentialLayout;
259 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.SequentialLayout);
263 public bool IsExplicitLayout {
264 get { return (m_attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
267 m_attributes &= ~TypeAttributes.LayoutMask;
268 m_attributes |= TypeAttributes.ExplicitLayout;
270 m_attributes &= ~(TypeAttributes.LayoutMask & TypeAttributes.ExplicitLayout);
274 public bool IsClass {
275 get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Class; }
278 m_attributes &= ~TypeAttributes.ClassSemanticMask;
279 m_attributes |= TypeAttributes.Class;
281 m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Class);
285 public bool IsInterface {
286 get { return (m_attributes & TypeAttributes.ClassSemanticMask) == TypeAttributes.Interface; }
289 m_attributes &= ~TypeAttributes.ClassSemanticMask;
290 m_attributes |= TypeAttributes.Interface;
292 m_attributes &= ~(TypeAttributes.ClassSemanticMask & TypeAttributes.Interface);
296 public bool IsAbstract {
297 get { return (m_attributes & TypeAttributes.Abstract) != 0; }
300 m_attributes |= TypeAttributes.Abstract;
302 m_attributes &= ~TypeAttributes.Abstract;
306 public bool IsSealed {
307 get { return (m_attributes & TypeAttributes.Sealed) != 0; }
310 m_attributes |= TypeAttributes.Sealed;
312 m_attributes &= ~TypeAttributes.Sealed;
316 public bool IsSpecialName {
317 get { return (m_attributes & TypeAttributes.SpecialName) != 0; }
320 m_attributes |= TypeAttributes.SpecialName;
322 m_attributes &= ~TypeAttributes.SpecialName;
326 public bool IsImport {
327 get { return (m_attributes & TypeAttributes.Import) != 0; }
330 m_attributes |= TypeAttributes.Import;
332 m_attributes &= ~TypeAttributes.Import;
336 public bool IsSerializable {
337 get { return (m_attributes & TypeAttributes.Serializable) != 0; }
340 m_attributes |= TypeAttributes.Serializable;
342 m_attributes &= ~TypeAttributes.Serializable;
346 public bool IsAnsiClass {
347 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
350 m_attributes &= ~TypeAttributes.StringFormatMask;
351 m_attributes |= TypeAttributes.AnsiClass;
353 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AnsiClass);
357 public bool IsUnicodeClass {
358 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
361 m_attributes &= ~TypeAttributes.StringFormatMask;
362 m_attributes |= TypeAttributes.UnicodeClass;
364 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.UnicodeClass);
368 public bool IsAutoClass {
369 get { return (m_attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
372 m_attributes &= ~TypeAttributes.StringFormatMask;
373 m_attributes |= TypeAttributes.AutoClass;
375 m_attributes &= ~(TypeAttributes.StringFormatMask & TypeAttributes.AutoClass);
379 public bool IsBeforeFieldInit {
380 get { return (m_attributes & TypeAttributes.BeforeFieldInit) != 0; }
383 m_attributes |= TypeAttributes.BeforeFieldInit;
385 m_attributes &= ~TypeAttributes.BeforeFieldInit;
389 public bool IsRuntimeSpecialName {
390 get { return (m_attributes & TypeAttributes.RTSpecialName) != 0; }
393 m_attributes |= TypeAttributes.RTSpecialName;
395 m_attributes &= ~TypeAttributes.RTSpecialName;
399 public bool HasSecurity {
400 get { return (m_attributes & TypeAttributes.HasSecurity) != 0; }
403 m_attributes |= TypeAttributes.HasSecurity;
405 m_attributes &= ~TypeAttributes.HasSecurity;
412 get { return m_baseType != null && m_baseType.FullName == Constants.Enum; }
415 public override bool IsValueType {
417 return m_baseType != null && (
418 this.IsEnum || (m_baseType.FullName == Constants.ValueType && this.FullName != Constants.Enum));
422 internal TypeDefinition (string name, string ns, TypeAttributes attrs) :
426 m_attributes = attrs;
429 public TypeDefinition (string name, string ns,
430 TypeAttributes attributes, TypeReference baseType) :
431 this (name, ns, attributes)
433 this.BaseType = baseType;
436 public TypeDefinition Clone ()
438 return Clone (this, new ImportContext (NullReferenceImporter.Instance, this));
441 internal static TypeDefinition Clone (TypeDefinition type, ImportContext context)
443 TypeDefinition nt = new TypeDefinition (
448 context.GenericContext.Type = nt;
450 GenericParameter.CloneInto (type, nt, context);
452 if (type.BaseType != null)
453 nt.BaseType = context.Import (type.BaseType);
455 if (type.HasLayoutInfo) {
456 nt.ClassSize = type.ClassSize;
457 nt.PackingSize = type.PackingSize;
460 foreach (FieldDefinition field in type.Fields)
461 nt.Fields.Add (FieldDefinition.Clone (field, context));
462 foreach (MethodDefinition ctor in type.Constructors)
463 nt.Constructors.Add (MethodDefinition.Clone (ctor, context));
464 foreach (MethodDefinition meth in type.Methods)
465 nt.Methods.Add (MethodDefinition.Clone (meth, context));
466 foreach (EventDefinition evt in type.Events)
467 nt.Events.Add (EventDefinition.Clone (evt, context));
468 foreach (PropertyDefinition prop in type.Properties)
469 nt.Properties.Add (PropertyDefinition.Clone (prop, context));
470 foreach (TypeReference intf in type.Interfaces)
471 nt.Interfaces.Add (context.Import (intf));
472 foreach (TypeDefinition nested in type.NestedTypes)
473 nt.NestedTypes.Add (Clone (nested, context));
474 foreach (CustomAttribute ca in type.CustomAttributes)
475 nt.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
476 foreach (SecurityDeclaration dec in type.SecurityDeclarations)
477 nt.SecurityDeclarations.Add (SecurityDeclaration.Clone (dec));
482 public override void Accept (IReflectionVisitor visitor)
484 visitor.VisitTypeDefinition (this);
486 this.GenericParameters.Accept (visitor);
487 this.Interfaces.Accept (visitor);
488 this.Constructors.Accept (visitor);
489 this.Methods.Accept (visitor);
490 this.Fields.Accept (visitor);
491 this.Properties.Accept (visitor);
492 this.Events.Accept (visitor);
493 this.NestedTypes.Accept (visitor);
494 this.CustomAttributes.Accept (visitor);
495 this.SecurityDeclarations.Accept (visitor);