Ejemplo n.º 1
0
        public static DisassembledAssembly DisassembleAssembly(string assemblyPath)
        {
            //  Create a disassembled assembly.
            var disassembledAssembly = new DisassembledAssembly();

            //  Set the key properties.
            disassembledAssembly.AssemblyPath = assemblyPath;

            //  Get the assembly name.
            try
            {
                var assembly = Assembly.LoadFile(assemblyPath);
                disassembledAssembly.ShortName = assembly.GetName().Name;
                disassembledAssembly.FullName  = assembly.GetName().FullName;
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException(string.Format("Failed to load the assembly '{0}', it may not be a valid assembly file.", assemblyPath), exception);
            }

            //  Create a temporary file path.
            var tempFilePath = Path.GetTempFileName();

            //  Create an ILDASM object.
            var ildasm = new ILDASM();

            ildasm.IncludeLineNumbers        = true;
            ildasm.IncludeOriginalSourceCode = true;
            ildasm.InputFilePath             = assemblyPath;
            ildasm.OutputFilePath            = tempFilePath;

            //  Disasseble the code.
            try
            {
                ildasm.Run();
            }
            catch (Exception exception)
            {
                //  Add detail and rethrow the exception.
                throw new InvalidOperationException(string.Format("Failed to disassemble '{0}'.", assemblyPath), exception);
            }

            //  Read the file.
            using (var stream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
                using (var reader = new StreamReader(stream))
                {
                    disassembledAssembly.RawIL = reader.ReadToEnd();
                }

            //  Clean up the temporary file.
            File.Delete(tempFilePath);

            //  We're done.
            return(disassembledAssembly);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialises disassembler the specified assembly path.
        /// </summary>
        /// <param name="assemblyPath">The assembly path.</param>
        private void Initialise(string assemblyPath)
        {
            //  Store the assembly path.
            this.assemblyPath = assemblyPath;

            //  Create a temporary file path.
            var tempFilePath = Path.GetTempFileName();

            //  Create an ILDASM object.
            var ildasm = new ILDASM();

            ildasm.IncludeLineNumbers        = true;
            ildasm.IncludeOriginalSourceCode = true;
            ildasm.InputFilePath             = assemblyPath;
            ildasm.OutputFilePath            = tempFilePath;

            //  Disasseble the code.
            try
            {
                ildasm.Run();
            }
            catch (Exception exception)
            {
                //  Add detail and rethrow the exception.
                throw new InvalidOperationException(string.Format("Failed to disassemble '{0}'.", assemblyPath), exception);
            }

            //  Read the file.
            using (var stream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
                using (var reader = new StreamReader(stream))
                {
                    rawIl = reader.ReadToEnd();
                }

            //  We're done.
        }