5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2010 Jb Evain
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.
31
using Mono.Collections.Generic;
33
namespace Mono.Cecil {
35
public sealed class TypeDefinition : TypeReference, IMemberDefinition, ISecurityDeclarationProvider {
38
TypeReference base_type;
39
internal Range fields_range;
40
internal Range methods_range;
42
short packing_size = Mixin.NotResolvedMarker;
43
int class_size = Mixin.NotResolvedMarker;
45
Collection<TypeReference> interfaces;
46
Collection<TypeDefinition> nested_types;
47
Collection<MethodDefinition> methods;
48
Collection<FieldDefinition> fields;
49
Collection<EventDefinition> events;
50
Collection<PropertyDefinition> properties;
51
Collection<CustomAttribute> custom_attributes;
52
Collection<SecurityDeclaration> security_declarations;
54
public TypeAttributes Attributes {
55
get { return (TypeAttributes) attributes; }
56
set { attributes = (uint) value; }
59
public TypeReference BaseType {
60
get { return base_type; }
61
set { base_type = value; }
66
if (packing_size != Mixin.NotResolvedMarker || class_size != Mixin.NotResolvedMarker)
70
packing_size = Mixin.NoDataMarker;
71
class_size = Mixin.NoDataMarker;
75
var row = Module.Read (this, (type, reader) => reader.ReadTypeLayout (type));
77
packing_size = row.Col1;
78
class_size = row.Col2;
81
public bool HasLayoutInfo {
83
if (packing_size >= 0 || class_size >= 0)
88
return packing_size >= 0 || class_size >= 0;
92
public short PackingSize {
94
if (packing_size >= 0)
99
return packing_size >= 0 ? packing_size : (short) -1;
101
set { packing_size = value; }
104
public int ClassSize {
111
return class_size >= 0 ? class_size : -1;
113
set { class_size = value; }
116
public bool HasInterfaces {
118
if (interfaces != null)
119
return interfaces.Count > 0;
122
return Module.Read (this, (type, reader) => reader.HasInterfaces (type));
128
public Collection<TypeReference> Interfaces {
130
if (interfaces != null)
134
return interfaces = Module.Read (this, (type, reader) => reader.ReadInterfaces (type));
136
return interfaces = new Collection<TypeReference> ();
140
public bool HasNestedTypes {
142
if (nested_types != null)
143
return nested_types.Count > 0;
146
return Module.Read (this, (type, reader) => reader.HasNestedTypes (type));
152
public Collection<TypeDefinition> NestedTypes {
154
if (nested_types != null)
158
return nested_types = Module.Read (this, (type, reader) => reader.ReadNestedTypes (type));
160
return nested_types = new MemberDefinitionCollection<TypeDefinition> (this);
164
internal new bool HasImage {
165
get { return Module != null && Module.HasImage; }
168
public bool HasMethods {
171
return methods.Count > 0;
174
return methods_range.Length > 0;
180
public Collection<MethodDefinition> Methods {
186
return methods = Module.Read (this, (type, reader) => reader.ReadMethods (type));
188
return methods = new MemberDefinitionCollection<MethodDefinition> (this);
192
public bool HasFields {
195
return fields.Count > 0;
198
return fields_range.Length > 0;
204
public Collection<FieldDefinition> Fields {
210
return fields = Module.Read (this, (type, reader) => reader.ReadFields (type));
212
return fields = new MemberDefinitionCollection<FieldDefinition> (this);
216
public bool HasEvents {
219
return events.Count > 0;
222
return Module.Read (this, (type, reader) => reader.HasEvents (type));
228
public Collection<EventDefinition> Events {
234
return events = Module.Read (this, (type, reader) => reader.ReadEvents (type));
236
return events = new MemberDefinitionCollection<EventDefinition> (this);
240
public bool HasProperties {
242
if (properties != null)
243
return properties.Count > 0;
246
return Module.Read (this, (type, reader) => reader.HasProperties (type));
252
public Collection<PropertyDefinition> Properties {
254
if (properties != null)
258
return properties = Module.Read (this, (type, reader) => reader.ReadProperties (type));
260
return properties = new MemberDefinitionCollection<PropertyDefinition> (this);
264
public bool HasSecurityDeclarations {
266
if (security_declarations != null)
267
return security_declarations.Count > 0;
269
return this.GetHasSecurityDeclarations (Module);
273
public Collection<SecurityDeclaration> SecurityDeclarations {
274
get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (Module)); }
277
public bool HasCustomAttributes {
279
if (custom_attributes != null)
280
return custom_attributes.Count > 0;
282
return this.GetHasCustomAttributes (Module);
286
public Collection<CustomAttribute> CustomAttributes {
287
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
290
public override bool HasGenericParameters {
292
if (generic_parameters != null)
293
return generic_parameters.Count > 0;
295
return this.GetHasGenericParameters (Module);
299
public override Collection<GenericParameter> GenericParameters {
300
get { return generic_parameters ?? (generic_parameters = this.GetGenericParameters (Module)); }
303
#region TypeAttributes
305
public bool IsNotPublic {
306
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic); }
307
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NotPublic, value); }
310
public bool IsPublic {
311
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public); }
312
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.Public, value); }
315
public bool IsNestedPublic {
316
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic); }
317
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPublic, value); }
320
public bool IsNestedPrivate {
321
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate); }
322
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedPrivate, value); }
325
public bool IsNestedFamily {
326
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily); }
327
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamily, value); }
330
public bool IsNestedAssembly {
331
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly); }
332
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedAssembly, value); }
335
public bool IsNestedFamilyAndAssembly {
336
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem); }
337
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamANDAssem, value); }
340
public bool IsNestedFamilyOrAssembly {
341
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem); }
342
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.VisibilityMask, (uint) TypeAttributes.NestedFamORAssem, value); }
345
public bool IsAutoLayout {
346
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout); }
347
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.AutoLayout, value); }
350
public bool IsSequentialLayout {
351
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout); }
352
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.SequentialLayout, value); }
355
public bool IsExplicitLayout {
356
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout); }
357
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.LayoutMask, (uint) TypeAttributes.ExplicitLayout, value); }
360
public bool IsClass {
361
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class); }
362
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Class, value); }
365
public bool IsInterface {
366
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface); }
367
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.ClassSemanticMask, (uint) TypeAttributes.Interface, value); }
370
public bool IsAbstract {
371
get { return attributes.GetAttributes ((uint) TypeAttributes.Abstract); }
372
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Abstract, value); }
375
public bool IsSealed {
376
get { return attributes.GetAttributes ((uint) TypeAttributes.Sealed); }
377
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Sealed, value); }
380
public bool IsSpecialName {
381
get { return attributes.GetAttributes ((uint) TypeAttributes.SpecialName); }
382
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.SpecialName, value); }
385
public bool IsImport {
386
get { return attributes.GetAttributes ((uint) TypeAttributes.Import); }
387
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Import, value); }
390
public bool IsSerializable {
391
get { return attributes.GetAttributes ((uint) TypeAttributes.Serializable); }
392
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.Serializable, value); }
395
public bool IsAnsiClass {
396
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass); }
397
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AnsiClass, value); }
400
public bool IsUnicodeClass {
401
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass); }
402
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.UnicodeClass, value); }
405
public bool IsAutoClass {
406
get { return attributes.GetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass); }
407
set { attributes = attributes.SetMaskedAttributes ((uint) TypeAttributes.StringFormatMask, (uint) TypeAttributes.AutoClass, value); }
410
public bool IsBeforeFieldInit {
411
get { return attributes.GetAttributes ((uint) TypeAttributes.BeforeFieldInit); }
412
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.BeforeFieldInit, value); }
415
public bool IsRuntimeSpecialName {
416
get { return attributes.GetAttributes ((uint) TypeAttributes.RTSpecialName); }
417
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.RTSpecialName, value); }
420
public bool HasSecurity {
421
get { return attributes.GetAttributes ((uint) TypeAttributes.HasSecurity); }
422
set { attributes = attributes.SetAttributes ((uint) TypeAttributes.HasSecurity, value); }
428
get { return base_type != null && base_type.IsTypeOf ("System", "Enum"); }
431
public override bool IsValueType {
433
if (base_type == null)
436
return base_type.IsTypeOf ("System", "Enum") || (base_type.IsTypeOf ("System", "ValueType") && !this.IsTypeOf ("System", "Enum"));
440
public override bool IsDefinition {
444
public new TypeDefinition DeclaringType {
445
get { return (TypeDefinition) base.DeclaringType; }
446
set { base.DeclaringType = value; }
449
public TypeDefinition (string @namespace, string name, TypeAttributes attributes)
450
: base (@namespace, name)
452
this.attributes = (uint) attributes;
453
this.token = new MetadataToken (TokenType.TypeDef);
456
public TypeDefinition (string @namespace, string name, TypeAttributes attributes, TypeReference baseType) :
457
this (@namespace, name, attributes)
459
this.BaseType = baseType;
462
public override TypeDefinition Resolve ()
468
static partial class Mixin {
470
public static TypeReference GetEnumUnderlyingType (this TypeDefinition self)
472
var fields = self.Fields;
474
for (int i = 0; i < fields.Count; i++) {
475
var field = fields [i];
477
return field.FieldType;
480
throw new ArgumentException ();
483
public static TypeDefinition GetNestedType (this TypeDefinition self, string name)
485
if (!self.HasNestedTypes)
488
var nested_types = self.NestedTypes;
490
for (int i = 0; i < nested_types.Count; i++) {
491
var nested_type = nested_types [i];
492
if (nested_type.Name == name)