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;
96
if (corlib.Major == 1)
97
return corlib.Minor == 0 ? TargetRuntime.NET_1_0 : TargetRuntime.NET_1_1;
98
else // if (corlib.Major == 2)
99
return TargetRuntime.NET_2_0;
102
public static AssemblyDefinition DefineAssembly (string name, AssemblyKind kind)
104
return DefineAssembly (name, name, CurrentRuntime (), kind);
107
public static AssemblyDefinition DefineAssembly (string name, TargetRuntime rt, AssemblyKind kind)
109
return DefineAssembly (name, name, rt, kind);
112
public static AssemblyDefinition DefineAssembly (string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
114
AssemblyNameDefinition asmName = new AssemblyNameDefinition ();
115
asmName.Name = assemblyName;
116
AssemblyDefinition asm = new AssemblyDefinition (asmName);
119
ModuleDefinition main = new ModuleDefinition (moduleName, asm, true);
120
asm.Modules.Add (main);
124
static void WriteAssembly (AssemblyDefinition asm, BinaryWriter bw)
126
asm.Accept (new StructureWriter (asm, bw));
129
public static void SaveAssembly (AssemblyDefinition asm, string file)
131
using (FileStream fs = new FileStream (
132
file, FileMode.Create, FileAccess.Write, FileShare.None)) {
134
SaveAssembly (asm, fs);
135
asm.MainModule.Image.SetFileInfo (new FileInfo (file));
139
public static void SaveAssembly (AssemblyDefinition asm, out byte [] assembly)
141
MemoryBinaryWriter bw = new MemoryBinaryWriter ();
142
SaveAssembly (asm, bw.BaseStream);
143
assembly = bw.ToArray ();
146
public static void SaveAssembly (AssemblyDefinition asm, Stream stream)
148
BinaryWriter bw = new BinaryWriter (stream);
150
WriteAssembly (asm, bw);
155
foreach (ModuleDefinition module in asm.Modules)
156
if (module.Controller.Writer.SaveSymbols)
157
module.Controller.Writer.WriteSymbols (module);
160
#if !CF_1_0 && !CF_2_0
161
public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm, AppDomain domain)
163
using (MemoryBinaryWriter writer = new MemoryBinaryWriter ()) {
165
WriteAssembly (asm, writer);
166
return domain.Load (writer.ToArray ());
170
public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm)
172
return CreateReflectionAssembly (asm, AppDomain.CurrentDomain);