PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Mono.Cecil/AssemblyDefinition.cs

http://github.com/jbevain/cecil
C# | 189 lines | 142 code | 38 blank | 9 comment | 19 complexity | c6a5e8babe58cd0c0920522a63959a4b MD5 | raw file
  1//
  2// Author:
  3//   Jb Evain (jbevain@gmail.com)
  4//
  5// Copyright (c) 2008 - 2015 Jb Evain
  6// Copyright (c) 2008 - 2011 Novell, Inc.
  7//
  8// Licensed under the MIT/X11 license.
  9//
 10
 11using System;
 12using System.IO;
 13using System.Threading;
 14using Mono.Collections.Generic;
 15
 16namespace Mono.Cecil {
 17
 18	public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider, IDisposable {
 19
 20		AssemblyNameDefinition name;
 21
 22		internal ModuleDefinition main_module;
 23		Collection<ModuleDefinition> modules;
 24		Collection<CustomAttribute> custom_attributes;
 25		Collection<SecurityDeclaration> security_declarations;
 26
 27		public AssemblyNameDefinition Name {
 28			get { return name; }
 29			set { name = value; }
 30		}
 31
 32		public string FullName {
 33			get { return name != null ? name.FullName : string.Empty; }
 34		}
 35
 36		public MetadataToken MetadataToken {
 37			get { return new MetadataToken (TokenType.Assembly, 1); }
 38			set { }
 39		}
 40
 41		public Collection<ModuleDefinition> Modules {
 42			get {
 43				if (modules != null)
 44					return modules;
 45
 46				if (main_module.HasImage)
 47					return main_module.Read (ref modules, this, (_, reader) => reader.ReadModules ());
 48
 49				Interlocked.CompareExchange (ref modules, new Collection<ModuleDefinition> (1) { main_module }, null);
 50				return modules;
 51			}
 52		}
 53
 54		public ModuleDefinition MainModule {
 55			get { return main_module; }
 56		}
 57
 58		public MethodDefinition EntryPoint {
 59			get { return main_module.EntryPoint; }
 60			set { main_module.EntryPoint = value; }
 61		}
 62
 63		public bool HasCustomAttributes {
 64			get {
 65				if (custom_attributes != null)
 66					return custom_attributes.Count > 0;
 67
 68				return this.GetHasCustomAttributes (main_module);
 69			}
 70		}
 71
 72		public Collection<CustomAttribute> CustomAttributes {
 73			get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, main_module)); }
 74		}
 75
 76		public bool HasSecurityDeclarations {
 77			get {
 78				if (security_declarations != null)
 79					return security_declarations.Count > 0;
 80
 81				return this.GetHasSecurityDeclarations (main_module);
 82			}
 83		}
 84
 85		public Collection<SecurityDeclaration> SecurityDeclarations {
 86			get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, main_module)); }
 87		}
 88
 89		internal AssemblyDefinition ()
 90		{
 91		}
 92
 93		public void Dispose ()
 94		{
 95			if (this.modules == null) {
 96				main_module.Dispose ();
 97				return;
 98			}
 99
100			var modules = this.Modules;
101			for (int i = 0; i < modules.Count; i++)
102				modules [i].Dispose ();
103		}
104		public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind)
105		{
106			return CreateAssembly (assemblyName, moduleName, new ModuleParameters { Kind = kind });
107		}
108
109		public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
110		{
111			if (assemblyName == null)
112				throw new ArgumentNullException ("assemblyName");
113			if (moduleName == null)
114				throw new ArgumentNullException ("moduleName");
115			Mixin.CheckParameters (parameters);
116			if (parameters.Kind == ModuleKind.NetModule)
117				throw new ArgumentException ("kind");
118
119			var assembly = ModuleDefinition.CreateModule (moduleName, parameters).Assembly;
120			assembly.Name = assemblyName;
121
122			return assembly;
123		}
124
125		public static AssemblyDefinition ReadAssembly (string fileName)
126		{
127			return ReadAssembly (ModuleDefinition.ReadModule (fileName));
128		}
129
130		public static AssemblyDefinition ReadAssembly (string fileName, ReaderParameters parameters)
131		{
132			return ReadAssembly (ModuleDefinition.ReadModule (fileName, parameters));
133		}
134
135		public static AssemblyDefinition ReadAssembly (Stream stream)
136		{
137			return ReadAssembly (ModuleDefinition.ReadModule (stream));
138		}
139
140		public static AssemblyDefinition ReadAssembly (Stream stream, ReaderParameters parameters)
141		{
142			return ReadAssembly (ModuleDefinition.ReadModule (stream, parameters));
143		}
144
145		static AssemblyDefinition ReadAssembly (ModuleDefinition module)
146		{
147			var assembly = module.Assembly;
148			if (assembly == null)
149				throw new ArgumentException ();
150
151			return assembly;
152		}
153
154		public void Write (string fileName)
155		{
156			Write (fileName, new WriterParameters ());
157		}
158
159		public void Write (string fileName, WriterParameters parameters)
160		{
161			main_module.Write (fileName, parameters);
162		}
163
164		public void Write ()
165		{
166			main_module.Write ();
167		}
168
169		public void Write (WriterParameters parameters)
170		{
171			main_module.Write (parameters);
172		}
173
174		public void Write (Stream stream)
175		{
176			Write (stream, new WriterParameters ());
177		}
178
179		public void Write (Stream stream, WriterParameters parameters)
180		{
181			main_module.Write (stream, parameters);
182		}
183
184		public override string ToString ()
185		{
186			return this.FullName;
187		}
188	}
189}