Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version

Building .NET Coverage Tool

, 25 Aug 2009
This article is a walkthrough for building a .NET coverage tool
Coverage.NET_demo.zip
TestApp
TestApp.exe
TestApp.pdb
Tool
Coverage.Counter.dll
Coverage.exe
Mono.Cecil.dll
Mono.Tools.Pdb2Mdb.dll
Coverage.NET_src.zip
Coverage
Common
Instrument
Properties
Report
Coverage.Counter
Properties
Mono.Cecil
src
AUTHORS
ChangeLog
CodeGen
cecil-gen-attributes.rb
cecil-gen-sources.rb
cecil-gen-tests.rb
cecil-gen-types.rb
cecil-gen.rb
cecil-mig.rb
cecil-update-rev
templates
configure
default.build
Makefile
mono-cecil.pc.in
Mono.Cecil
Mono.Cecil.Binary
Mono.Cecil.Cil
Mono.Cecil.dll.sources
Mono.Cecil.Metadata
Mono.Cecil.Signatures
Mono.Xml
ChangeLog
NEWS
README
standalone.make
TODO
Mono.Tools.Pdb2Mdb
Properties
src
ChangeLog
LICENSE
Makefile
pdb2mdb.exe.sources
//
// AssemblyFactory.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

namespace Mono.Cecil {

	using System;
	using System.IO;
	using SR = System.Reflection;

	using Mono.Cecil.Binary;

	public sealed class AssemblyFactory {

		AssemblyFactory ()
		{
		}

		static AssemblyDefinition GetAssembly (ImageReader irv, bool manifestOnly)
		{
			StructureReader srv = new StructureReader (irv, manifestOnly);
			AssemblyDefinition asm = new AssemblyDefinition (
				new AssemblyNameDefinition (), srv);

			asm.Accept (srv);
			return asm;
		}

		static AssemblyDefinition GetAssembly (ImageReader reader)
		{
			return GetAssembly (reader, false);
		}

		static AssemblyDefinition GetAssemblyManifest (ImageReader reader)
		{
			return GetAssembly (reader, true);
		}

		public static AssemblyDefinition GetAssembly (string file)
		{
			return GetAssembly (ImageReader.Read (file));
		}

		public static AssemblyDefinition GetAssembly (byte [] assembly)
		{
			return GetAssembly (ImageReader.Read (assembly));
		}

		public static AssemblyDefinition GetAssembly (Stream stream)
		{
			return GetAssembly (ImageReader.Read (stream));
		}

		public static AssemblyDefinition GetAssemblyManifest (string file)
		{
			return GetAssemblyManifest (ImageReader.Read (file));
		}

		public static AssemblyDefinition GetAssemblyManifest (byte [] assembly)
		{
			return GetAssemblyManifest (ImageReader.Read (assembly));
		}

		public static AssemblyDefinition GetAssemblyManifest (Stream stream)
		{
			return GetAssemblyManifest (ImageReader.Read (stream));
		}

		static TargetRuntime CurrentRuntime ()
		{
			Version corlib = typeof (object).Assembly.GetName ().Version;

			switch (corlib.Major) {
			case 1:
				return corlib.Minor == 0 ? TargetRuntime.NET_1_0 : TargetRuntime.NET_1_1;
			case 2:
				return TargetRuntime.NET_2_0;
			case 4:
				return TargetRuntime.NET_4_0;
			default:
				throw new NotSupportedException ();
			}
		}

		public static AssemblyDefinition DefineAssembly (string name, AssemblyKind kind)
		{
			return DefineAssembly (name, name, CurrentRuntime (), kind);
		}

		public static AssemblyDefinition DefineAssembly (string name, TargetRuntime rt, AssemblyKind kind)
		{
			return DefineAssembly (name, name, rt, kind);
		}

		public static AssemblyDefinition DefineAssembly (string assemblyName, string moduleName, TargetRuntime rt, AssemblyKind kind)
		{
			AssemblyNameDefinition asmName = new AssemblyNameDefinition ();
			asmName.Name = assemblyName;
			AssemblyDefinition asm = new AssemblyDefinition (asmName);
			asm.Runtime = rt;
			asm.Kind = kind;
			ModuleDefinition main = new ModuleDefinition (moduleName, asm, true);
			asm.Modules.Add (main);
			return asm;
		}

		static void WriteAssembly (AssemblyDefinition asm, BinaryWriter bw)
		{
			asm.Accept (new StructureWriter (asm, bw));
		}

		public static void SaveAssembly (AssemblyDefinition asm, string file)
		{
			using (FileStream fs = new FileStream (
				file, FileMode.Create, FileAccess.Write, FileShare.None)) {

				SaveAssembly (asm, fs);
				asm.MainModule.Image.SetFileInfo (new FileInfo (file));
			}
		}

		public static void SaveAssembly (AssemblyDefinition asm, out byte [] assembly)
		{
			MemoryBinaryWriter bw = new MemoryBinaryWriter ();
			SaveAssembly (asm, bw.BaseStream);
			assembly = bw.ToArray ();
		}

		public static void SaveAssembly (AssemblyDefinition asm, Stream stream)
		{
			BinaryWriter bw = new BinaryWriter (stream);
			try {
				WriteAssembly (asm, bw);
			} finally {
				bw.Close ();
			}

			foreach (ModuleDefinition module in asm.Modules)
				if (module.Controller.Writer.SaveSymbols)
					module.Controller.Writer.WriteSymbols (module);
		}

#if !CF_1_0 && !CF_2_0
		public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm, AppDomain domain)
		{
			using (MemoryBinaryWriter writer = new MemoryBinaryWriter ()) {

				WriteAssembly (asm, writer);
				return domain.Load (writer.ToArray ());
			}
		}

		public static SR.Assembly CreateReflectionAssembly (AssemblyDefinition asm)
		{
			return CreateReflectionAssembly (asm, AppDomain.CurrentDomain);
		}
#endif
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Sergiy Sakharov
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140823.1 | Last Updated 25 Aug 2009
Article Copyright 2009 by Sergiy Sakharov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid