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 {
33 using SR = System.Reflection;
35 using Mono.Cecil.Binary;
37 public sealed class AssemblyFactory {
43 static AssemblyDefinition GetAssembly (ImageReader irv, bool manifestOnly)
45 StructureReader srv = new StructureReader (irv, manifestOnly);
46 AssemblyDefinition asm = new AssemblyDefinition (
47 new AssemblyNameDefinition (), srv);
53 static AssemblyDefinition GetAssembly (ImageReader reader)
55 return GetAssembly (reader, false);
58 static AssemblyDefinition GetAssemblyManifest (ImageReader reader)
60 return GetAssembly (reader, true);
63 public static AssemblyDefinition GetAssembly (string file)
65 return GetAssembly (ImageReader.Read (file));
68 public static AssemblyDefinition GetAssembly (byte [] assembly)
70 return GetAssembly (ImageReader.Read (assembly));
73 public static AssemblyDefinition GetAssembly (Stream stream)
75 return GetAssembly (ImageReader.Read (stream));
78 public static AssemblyDefinition GetAssemblyManifest (string file)
80 return GetAssemblyManifest (ImageReader.Read (file));
83 public static AssemblyDefinition GetAssemblyManifest (byte [] assembly)
85 return GetAssemblyManifest (ImageReader.Read (assembly));
88 public static AssemblyDefinition GetAssemblyManifest (Stream stream)
90 return GetAssemblyManifest (ImageReader.Read (stream));
93 static TargetRuntime CurrentRuntime ()
95 Version corlib = typeof (object).Assembly.GetName ().Version;
97 switch (corlib.Major) {
99 return corlib.Minor == 0 ? TargetRuntime.NET_1_0 : TargetRuntime.NET_1_1;
101 return TargetRuntime.NET_2_0;
103 return TargetRuntime.NET_4_0;
105 throw new NotSupportedException ();
109 public static AssemblyDefinition DefineAssembly (string name, AssemblyKind kind)
111 return DefineAssembly (name, name, CurrentRuntime (), kind);
114 public static AssemblyDefinition DefineAssembly (string name, TargetRuntime rt, AssemblyKind kind)
116 return DefineAssembly (name, name, rt, kind);
119 public static AssemblyDefinition DefineAssembly (string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
121 AssemblyNameDefinition asmName = new AssemblyNameDefinition ();
122 asmName.Name = assemblyName;
123 AssemblyDefinition asm = new AssemblyDefinition (asmName);
126 ModuleDefinition main = new ModuleDefinition (moduleName, asm, true);
127 asm.Modules.Add (main);
131 static void WriteAssembly (AssemblyDefinition asm, BinaryWriter bw)
133 asm.Accept (new StructureWriter (asm, bw));
136 public static void SaveAssembly (AssemblyDefinition asm, string file)
138 using (FileStream fs = new FileStream (
139 file, FileMode.Create, FileAccess.Write, FileShare.None)) {
141 SaveAssembly (asm, fs);
142 asm.MainModule.Image.SetFileInfo (new FileInfo (file));
146 public static void SaveAssembly (AssemblyDefinition asm, out byte [] assembly)
148 MemoryBinaryWriter bw = new MemoryBinaryWriter ();
149 SaveAssembly (asm, bw.BaseStream);
150 assembly = bw.ToArray ();
153 public static void SaveAssembly (AssemblyDefinition asm, Stream stream)
155 BinaryWriter bw = new BinaryWriter (stream);
157 WriteAssembly (asm, bw);
162 foreach (ModuleDefinition module in asm.Modules)
163 if (module.Controller.Writer.SaveSymbols)
164 module.Controller.Writer.WriteSymbols (module);
167 #if !CF_1_0 && !CF_2_0
168 public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm, AppDomain domain)
170 using (MemoryBinaryWriter writer = new MemoryBinaryWriter ()) {
172 WriteAssembly (asm, writer);
173 return domain.Load (writer.ToArray ());
177 public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm)
179 return CreateReflectionAssembly (asm, AppDomain.CurrentDomain);