2 // AssemblyDefinition.cs
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.
32 using Mono.Collections.Generic;
34 namespace Mono.Cecil {
36 public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider {
38 AssemblyNameDefinition name;
40 internal ModuleDefinition main_module;
41 Collection<ModuleDefinition> modules;
42 Collection<CustomAttribute> custom_attributes;
43 Collection<SecurityDeclaration> security_declarations;
45 public AssemblyNameDefinition Name {
50 public string FullName {
51 get { return name != null ? name.FullName : string.Empty; }
54 public MetadataToken MetadataToken {
55 get { return new MetadataToken (TokenType.Assembly, 1); }
59 public Collection<ModuleDefinition> Modules {
64 if (main_module.HasImage)
65 return modules = main_module.Read (this, (_, reader) => reader.ReadModules ());
67 return modules = new Collection<ModuleDefinition> { main_module };
71 public ModuleDefinition MainModule {
72 get { return main_module; }
75 public MethodDefinition EntryPoint {
76 get { return main_module.EntryPoint; }
77 set { main_module.EntryPoint = value; }
80 public bool HasCustomAttributes {
82 if (custom_attributes != null)
83 return custom_attributes.Count > 0;
85 return this.GetHasCustomAttributes (main_module);
89 public Collection<CustomAttribute> CustomAttributes {
90 get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (main_module)); }
93 public bool HasSecurityDeclarations {
95 if (security_declarations != null)
96 return security_declarations.Count > 0;
98 return this.GetHasSecurityDeclarations (main_module);
102 public Collection<SecurityDeclaration> SecurityDeclarations {
103 get { return security_declarations ?? (security_declarations = this.GetSecurityDeclarations (main_module)); }
106 internal AssemblyDefinition ()
111 public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind)
113 return CreateAssembly (assemblyName, moduleName, new ModuleParameters { Kind = kind });
116 public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
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");
126 var assembly = ModuleDefinition.CreateModule (moduleName, parameters).Assembly;
127 assembly.Name = assemblyName;
133 public static AssemblyDefinition ReadAssembly (string fileName)
135 return ReadAssembly (ModuleDefinition.ReadModule (fileName));
138 public static AssemblyDefinition ReadAssembly (string fileName, ReaderParameters parameters)
140 return ReadAssembly (ModuleDefinition.ReadModule (fileName, parameters));
143 public static AssemblyDefinition ReadAssembly (Stream stream)
145 return ReadAssembly (ModuleDefinition.ReadModule (stream));
148 public static AssemblyDefinition ReadAssembly (Stream stream, ReaderParameters parameters)
150 return ReadAssembly (ModuleDefinition.ReadModule (stream, parameters));
153 static AssemblyDefinition ReadAssembly (ModuleDefinition module)
155 var assembly = module.Assembly;
156 if (assembly == null)
157 throw new ArgumentException ();
163 public void Write (string fileName)
165 Write (fileName, new WriterParameters ());
168 public void Write (Stream stream)
170 Write (stream, new WriterParameters ());
173 public void Write (string fileName, WriterParameters parameters)
175 main_module.Write (fileName, parameters);
178 public void Write (Stream stream, WriterParameters parameters)
180 main_module.Write (stream, parameters);
184 public override string ToString ()
186 return this.FullName;