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.NotPublic) != 0; }
156 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NotPublic);
158 m_attributes |= masked;
160 m_attributes &= masked;
164 public bool IsPublic {
165 get { return (m_attributes & TypeAttributes.Public) != 0; }
167 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.Public);
169 m_attributes |= masked;
171 m_attributes &= masked;
175 public bool IsNestedPublic {
176 get { return (m_attributes & TypeAttributes.NestedPublic) != 0; }
178 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedPublic);
180 m_attributes |= masked;
182 m_attributes &= masked;
186 public bool IsNestedPrivate {
187 get { return (m_attributes & TypeAttributes.NestedPrivate) != 0; }
189 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedPrivate);
191 m_attributes |= masked;
193 m_attributes &= masked;
197 public bool IsNestedFamily {
198 get { return (m_attributes & TypeAttributes.NestedFamily) != 0; }
200 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedFamily);
202 m_attributes |= masked;
204 m_attributes &= masked;
208 public bool IsNestedAssembly {
209 get { return (m_attributes & TypeAttributes.NestedAssembly) != 0; }
211 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedAssembly);
213 m_attributes |= masked;
215 m_attributes &= masked;
219 public bool IsNestedFamilyAndAssembly {
220 get { return (m_attributes & TypeAttributes.NestedFamANDAssem) != 0; }
222 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedFamANDAssem);
224 m_attributes |= masked;
226 m_attributes &= masked;
230 public bool IsNestedFamilyOrAssembly {
231 get { return (m_attributes & TypeAttributes.NestedFamORAssem) != 0; }
233 TypeAttributes masked = (TypeAttributes.VisibilityMask & TypeAttributes.NestedFamORAssem);
235 m_attributes |= masked;
237 m_attributes &= masked;
241 public bool IsAutoLayout {
242 get { return (m_attributes & TypeAttributes.AutoLayout) != 0; }
244 TypeAttributes masked = (TypeAttributes.LayoutMask & TypeAttributes.AutoLayout);
246 m_attributes |= masked;
248 m_attributes &= masked;
252 public bool IsSequentialLayout {
253 get { return (m_attributes & TypeAttributes.SequentialLayout) != 0; }
255 TypeAttributes masked = (TypeAttributes.LayoutMask & TypeAttributes.SequentialLayout);
257 m_attributes |= masked;
259 m_attributes &= masked;
263 public bool IsExplicitLayout {
264 get { return (m_attributes & TypeAttributes.ExplicitLayout) != 0; }
266 TypeAttributes masked = (TypeAttributes.LayoutMask & TypeAttributes.ExplicitLayout);
268 m_attributes |= masked;
270 m_attributes &= masked;
274 public bool IsClass {
275 get { return (m_attributes & TypeAttributes.Class) != 0; }
277 TypeAttributes masked = (TypeAttributes.ClassSemanticMask & TypeAttributes.Class);
279 m_attributes |= masked;
281 m_attributes &= masked;
285 public bool IsInterface {
286 get { return (m_attributes & TypeAttributes.Interface) != 0; }
288 TypeAttributes masked = (TypeAttributes.ClassSemanticMask & TypeAttributes.Interface);
290 m_attributes |= masked;
292 m_attributes &= masked;
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.AnsiClass) != 0; }
349 TypeAttributes masked = (TypeAttributes.StringFormatMask & TypeAttributes.AnsiClass);
351 m_attributes |= masked;
353 m_attributes &= masked;
357 public bool IsUnicodeClass {
358 get { return (m_attributes & TypeAttributes.UnicodeClass) != 0; }
360 TypeAttributes masked = (TypeAttributes.StringFormatMask & TypeAttributes.UnicodeClass);
362 m_attributes |= masked;
364 m_attributes &= masked;
368 public bool IsAutoClass {
369 get { return (m_attributes & TypeAttributes.AutoClass) != 0; }
371 TypeAttributes masked = (TypeAttributes.StringFormatMask & TypeAttributes.AutoClass);
373 m_attributes |= masked;
375 m_attributes &= masked;
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);
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 foreach (GenericParameter p in type.GenericParameters)
451 nt.GenericParameters.Add (GenericParameter.Clone (p, context));
453 if (type.BaseType != null)
454 nt.BaseType = context.Import (type.BaseType);
456 if (type.HasLayoutInfo) {
457 nt.ClassSize = type.ClassSize;
458 nt.PackingSize = type.PackingSize;
461 foreach (FieldDefinition field in type.Fields)
462 nt.Fields.Add (FieldDefinition.Clone (field, context));
463 foreach (MethodDefinition ctor in type.Constructors)
464 nt.Constructors.Add (MethodDefinition.Clone (ctor, context));
465 foreach (MethodDefinition meth in type.Methods)
466 nt.Methods.Add (MethodDefinition.Clone (meth, context));
467 foreach (EventDefinition evt in type.Events)
468 nt.Events.Add (EventDefinition.Clone (evt, context));
469 foreach (PropertyDefinition prop in type.Properties)
470 nt.Properties.Add (PropertyDefinition.Clone (prop, context));
471 foreach (TypeReference intf in type.Interfaces)
472 nt.Interfaces.Add (context.Import (intf));
473 foreach (TypeDefinition nested in type.NestedTypes)
474 nt.NestedTypes.Add (Clone (nested, context));
475 foreach (CustomAttribute ca in type.CustomAttributes)
476 nt.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
477 foreach (SecurityDeclaration dec in type.SecurityDeclarations)
478 nt.SecurityDeclarations.Add (SecurityDeclaration.Clone (dec));
483 public override void Accept (IReflectionVisitor visitor)
485 visitor.VisitTypeDefinition (this);
487 this.GenericParameters.Accept (visitor);
488 this.Interfaces.Accept (visitor);
489 this.Constructors.Accept (visitor);
490 this.Methods.Accept (visitor);
491 this.Fields.Accept (visitor);
492 this.Properties.Accept (visitor);
493 this.Events.Accept (visitor);
494 this.NestedTypes.Accept (visitor);
495 this.CustomAttributes.Accept (visitor);
496 this.SecurityDeclarations.Accept (visitor);