~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Mono.Cecil/Mono.Cecil/Mono.Cecil/AssemblyDefinition.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using System;
 
30
using System.IO;
 
31
 
 
32
using Mono.Collections.Generic;
 
33
 
29
34
namespace Mono.Cecil {
30
35
 
31
 
        using System;
32
 
        using System.Collections;
33
 
 
34
 
        using Mono.Cecil.Metadata;
35
 
 
36
 
        public class AssemblyDefinition : ICustomAttributeProvider,
37
 
                IHasSecurity, IAnnotationProvider, IReflectionStructureVisitable {
38
 
 
39
 
                MetadataToken m_token;
40
 
                AssemblyNameDefinition m_asmName;
41
 
                ModuleDefinitionCollection m_modules;
42
 
                SecurityDeclarationCollection m_secDecls;
43
 
                CustomAttributeCollection m_customAttrs;
44
 
                MethodDefinition m_ep;
45
 
                TargetRuntime m_runtime;
46
 
                AssemblyKind m_kind;
47
 
 
48
 
                ModuleDefinition m_mainModule;
49
 
                StructureReader m_reader;
50
 
 
51
 
                IAssemblyResolver m_resolver;
52
 
                IDictionary m_annotations;
 
36
        public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider {
 
37
 
 
38
                AssemblyNameDefinition name;
 
39
 
 
40
                internal ModuleDefinition main_module;
 
41
                Collection<ModuleDefinition> modules;
 
42
                Collection<CustomAttribute> custom_attributes;
 
43
                Collection<SecurityDeclaration> security_declarations;
 
44
 
 
45
                public AssemblyNameDefinition Name {
 
46
                        get { return name; }
 
47
                        set { name = value; }
 
48
                }
 
49
 
 
50
                public string FullName {
 
51
                        get { return name != null ? name.FullName : string.Empty; }
 
52
                }
53
53
 
54
54
                public MetadataToken MetadataToken {
55
 
                        get { return m_token; }
56
 
                        set { m_token = value; }
57
 
                }
58
 
 
59
 
                public AssemblyNameDefinition Name {
60
 
                        get { return m_asmName; }
61
 
                }
62
 
 
63
 
                public ModuleDefinitionCollection Modules {
64
 
                        get { return m_modules; }
 
55
                        get { return new MetadataToken (TokenType.Assembly, 1); }
 
56
                        set { }
 
57
                }
 
58
 
 
59
                public Collection<ModuleDefinition> Modules {
 
60
                        get {
 
61
                                if (modules != null)
 
62
                                        return modules;
 
63
 
 
64
                                if (main_module.HasImage)
 
65
                                        return modules = main_module.Read (this, (_, reader) => reader.ReadModules ());
 
66
 
 
67
                                return modules = new Collection<ModuleDefinition> { main_module };
 
68
                        }
 
69
                }
 
70
 
 
71
                public ModuleDefinition MainModule {
 
72
                        get { return main_module; }
 
73
                }
 
74
 
 
75
                public MethodDefinition EntryPoint {
 
76
                        get { return main_module.EntryPoint; }
 
77
                        set { main_module.EntryPoint = value; }
 
78
                }
 
79
 
 
80
                public bool HasCustomAttributes {
 
81
                        get {
 
82
                                if (custom_attributes != null)
 
83
                                        return custom_attributes.Count > 0;
 
84
 
 
85
                                return this.GetHasCustomAttributes (main_module);
 
86
                        }
 
87
                }
 
88
 
 
89
                public Collection<CustomAttribute> CustomAttributes {
 
90
                        get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (main_module)); }
65
91
                }
66
92
 
67
93
                public bool HasSecurityDeclarations {
68
 
                        get { return (m_secDecls == null) ? false : (m_secDecls.Count > 0); }
69
 
                }
70
 
 
71
 
                public SecurityDeclarationCollection SecurityDeclarations {
72
 
                        get {
73
 
                                if (m_secDecls == null)
74
 
                                        m_secDecls = new SecurityDeclarationCollection (this);
75
 
 
76
 
                                return m_secDecls;
77
 
                        }
78
 
                }
79
 
 
80
 
                public bool HasCustomAttributes {
81
 
                        get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
82
 
                }
83
 
 
84
 
                public CustomAttributeCollection CustomAttributes {
85
 
                        get {
86
 
                                if (m_customAttrs == null)
87
 
                                        m_customAttrs = new CustomAttributeCollection (this);
88
 
 
89
 
                                return m_customAttrs;
90
 
                        }
91
 
                }
92
 
 
93
 
                public MethodDefinition EntryPoint {
94
 
                        get { return m_ep; }
95
 
                        set { m_ep = value; }
96
 
                }
97
 
 
98
 
                public TargetRuntime Runtime {
99
 
                        get { return m_runtime; }
100
 
                        set { m_runtime = value; }
101
 
                }
102
 
 
103
 
                public AssemblyKind Kind {
104
 
                        get { return m_kind; }
105
 
                        set { m_kind = value; }
106
 
                }
107
 
 
108
 
                public ModuleDefinition MainModule {
109
 
                        get {
110
 
                                if (m_mainModule == null) {
111
 
                                        foreach (ModuleDefinition module in m_modules) {
112
 
                                                if (module.Main) {
113
 
                                                        m_mainModule = module;
114
 
                                                        break;
115
 
                                                }
116
 
                                        }
117
 
                                }
118
 
                                return m_mainModule;
119
 
                        }
120
 
                }
121
 
 
122
 
                internal StructureReader Reader {
123
 
                        get { return m_reader; }
124
 
                }
125
 
 
126
 
                public IAssemblyResolver Resolver {
127
 
                        get { return m_resolver; }
128
 
                        set { m_resolver = value; }
129
 
                }
130
 
 
131
 
                IDictionary IAnnotationProvider.Annotations {
132
 
                        get {
133
 
                                if (m_annotations == null)
134
 
                                        m_annotations = new Hashtable ();
135
 
                                return m_annotations;
136
 
                        }
137
 
                }
138
 
 
139
 
                internal AssemblyDefinition (AssemblyNameDefinition name)
140
 
                {
141
 
                        if (name == null)
142
 
                                throw new ArgumentNullException ("name");
143
 
 
144
 
                        m_asmName = name;
145
 
                        m_modules = new ModuleDefinitionCollection (this);
146
 
                        m_resolver = new DefaultAssemblyResolver ();
147
 
                }
148
 
 
149
 
                internal AssemblyDefinition (AssemblyNameDefinition name, StructureReader reader) : this (name)
150
 
                {
151
 
                        m_reader = reader;
152
 
                }
153
 
 
154
 
                public void Accept (IReflectionStructureVisitor visitor)
155
 
                {
156
 
                        visitor.VisitAssemblyDefinition (this);
157
 
 
158
 
                        m_asmName.Accept (visitor);
159
 
                        m_modules.Accept (visitor);
160
 
 
161
 
                        visitor.TerminateAssemblyDefinition (this);
162
 
                }
 
94
                        get {
 
95
                                if (security_declarations != null)
 
96
                                        return security_declarations.Count > 0;
 
97
 
 
98
                                return this.GetHasSecurityDeclarations (main_module);
 
99
                        }
 
100
                }
 
101
 
 
102
                public Collection<SecurityDeclaration> SecurityDeclarations {
 
103
                        get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (main_module)); }
 
104
                }
 
105
 
 
106
                internal AssemblyDefinition ()
 
107
                {
 
108
                }
 
109
 
 
110
#if !READ_ONLY
 
111
                public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind)
 
112
                {
 
113
                        return CreateAssembly (assemblyName, moduleName, new ModuleParameters { Kind = kind });
 
114
                }
 
115
 
 
116
                public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
 
117
                {
 
118
                        if (assemblyName == null)
 
119
                                throw new ArgumentNullException ("assemblyName");
 
120
                        if (moduleName == null)
 
121
                                throw new ArgumentNullException ("moduleName");
 
122
                        Mixin.CheckParameters (parameters);
 
123
                        if (parameters.Kind == ModuleKind.NetModule)
 
124
                                throw new ArgumentException ("kind");
 
125
 
 
126
                        var assembly = ModuleDefinition.CreateModule (moduleName, parameters).Assembly;
 
127
                        assembly.Name = assemblyName;
 
128
 
 
129
                        return assembly;
 
130
                }
 
131
#endif
 
132
 
 
133
                public static AssemblyDefinition ReadAssembly (string fileName)
 
134
                {
 
135
                        return ReadAssembly (ModuleDefinition.ReadModule (fileName));
 
136
                }
 
137
 
 
138
                public static AssemblyDefinition ReadAssembly (string fileName, ReaderParameters parameters)
 
139
                {
 
140
                        return ReadAssembly (ModuleDefinition.ReadModule (fileName, parameters));
 
141
                }
 
142
 
 
143
                public static AssemblyDefinition ReadAssembly (Stream stream)
 
144
                {
 
145
                        return ReadAssembly (ModuleDefinition.ReadModule (stream));
 
146
                }
 
147
 
 
148
                public static AssemblyDefinition ReadAssembly (Stream stream, ReaderParameters parameters)
 
149
                {
 
150
                        return ReadAssembly (ModuleDefinition.ReadModule (stream, parameters));
 
151
                }
 
152
 
 
153
                static AssemblyDefinition ReadAssembly (ModuleDefinition module)
 
154
                {
 
155
                        var assembly = module.Assembly;
 
156
                        if (assembly == null)
 
157
                                throw new ArgumentException ();
 
158
 
 
159
                        return assembly;
 
160
                }
 
161
 
 
162
#if !READ_ONLY
 
163
                public void Write (string fileName)
 
164
                {
 
165
                        Write (fileName, new WriterParameters ());
 
166
                }
 
167
 
 
168
                public void Write (Stream stream)
 
169
                {
 
170
                        Write (stream, new WriterParameters ());
 
171
                }
 
172
 
 
173
                public void Write (string fileName, WriterParameters parameters)
 
174
                {
 
175
                        main_module.Write (fileName, parameters);
 
176
                }
 
177
 
 
178
                public void Write (Stream stream, WriterParameters parameters)
 
179
                {
 
180
                        main_module.Write (stream, parameters);
 
181
                }
 
182
#endif
163
183
 
164
184
                public override string ToString ()
165
185
                {
166
 
                        return m_asmName.FullName;
 
186
                        return this.FullName;
167
187
                }
168
188
        }
169
189
}