Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / MSIL

Automated MSIL/PE verification using ILSpy

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Sep 2013CPOL2 min read 15.6K   174   5  
Use ILSpy to perform a more comprehensive check on Emitted assembiles
namespace Emit.Test
{
    using System;
    using System.IO;
    using System.Linq;

    using Mono.Cecil;

    using Xunit;

    /// <summary>
    /// Collection of tests to check the validity of the emmited dynamic methods generated at runtime.
    /// </summary>
    public class Serializer
    {
        [Fact]
        public void CheckWithLSpy()
        {
            // create the dynamic methods and store them in a DLL 1 per 
            // object and to run the IL analysis.
            var dllPath = Path.Combine(Environment.CurrentDirectory, "$_test.dll");
            TestTargets.CreateDLL(dllPath);
            
            var assemblyDefinition = AssemblyDefinition.ReadAssembly(dllPath, new ReaderParameters(ReadingMode.Immediate));
            var typeDef = assemblyDefinition.MainModule.GetType(string.Empty, "EmitClass");
            var emitCode = typeDef.Methods.First(m => m.Name == "EmitCode");

            PEVerifier.VerifyMethods(new[] { emitCode });
        }

        [Fact]
        public void CheckWithPEVerify()
        {
            var dllPath = Path.Combine(Environment.CurrentDirectory, "$_test.dll");
            TestTargets.CreateDLL(dllPath);
            
            PEVerifier.VerifyPEFile(dllPath);
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions