PageRenderTime 123ms CodeModel.GetById 41ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/src/LinFu.AOP/Loaders/AssemblyLoaderWithPdbSupport.cs

http://github.com/philiplaureano/LinFu
C# | 89 lines | 49 code | 12 blank | 28 comment | 5 complexity | 9f951fc55c5731e49aa3ed9485b8fd5e MD5 | raw file
 1using System;
 2using System.IO;
 3using System.Reflection;
 4using LinFu.Reflection;
 5
 6namespace LinFu.AOP.Cecil.Loaders
 7{
 8    /// <summary>
 9    ///     Represents an <see cref="AssemblyLoader" /> class that adds support for loading PDB files into memory every time an
10    ///     assembly is loaded into memory.
11    /// </summary>
12    public class AssemblyLoaderWithPdbSupport : AssemblyLoader
13    {
14        /// <summary>
15        ///     Initializes a new instance of the AssemblyLoaderWithPdbSupport class.
16        /// </summary>
17        /// <param name="loader">The <see cref="IAssemblyLoader" /> that will perform the actual load operation.</param>
18        public AssemblyLoaderWithPdbSupport(IAssemblyLoader loader)
19        {
20            AssemblyLoader = loader;
21        }
22
23        /// <summary>
24        ///     Gets or sets the value indicating the <see cref="IAssemblyLoader" /> instance that will be used to load assemblies
25        ///     into memory.
26        /// </summary>
27        public virtual IAssemblyLoader AssemblyLoader { get; set; }
28
29        /// <summary>
30        ///     Loads the target assembly (and its corresponding PDB file) into memory.
31        /// </summary>
32        /// <param name="assemblyFile">The full path and filename of the assembly to load.</param>
33        /// <returns>A loaded <see cref="Assembly" /> instance.</returns>
34        public sealed override Assembly Load(string assemblyFile)
35        {
36            if (AssemblyLoader == null)
37                throw new NullReferenceException("The AssemblyLoader property cannot be null");
38
39            var pdbFile = string.Format("{0}.pdb", Path.GetFileNameWithoutExtension(assemblyFile));
40
41            // Check to see if the symbols file is available
42            var hasSymbols = File.Exists(pdbFile);
43            var pdbFileExists = File.Exists(pdbFile);
44            Assembly result = null;
45
46            var pdbTempFileName = Path.GetTempFileName();
47            var assemblyBackupFile = Path.GetTempFileName();
48
49            try
50            {
51                // Save the old PDB file
52                if (pdbFileExists)
53                    File.Copy(pdbFile, pdbTempFileName, true);
54
55                // Save the assembly file                
56                File.Copy(assemblyFile, assemblyBackupFile, true);
57
58                // Do the actual load operation
59                result = AssemblyLoader.Load(assemblyFile);
60            }
61            finally
62            {
63                RemoveTemporaryFiles(assemblyFile, pdbFile, pdbTempFileName, assemblyBackupFile);
64            }
65
66            return result;
67        }
68
69        /// <summary>
70        ///     Removes the temporary backup files that were created during the load operation.
71        /// </summary>
72        /// <param name="assemblyFile">The full path and location of the original assembly file.</param>
73        /// <param name="pdbFile">The full path and location of the original PDB file.</param>
74        /// <param name="pdbTempFileName">The full path and location of the temporary pdb file.</param>
75        /// <param name="assemblyBackupFile">The full path and location of the backup assembly file.</param>
76        private static void RemoveTemporaryFiles(string assemblyFile, string pdbFile, string pdbTempFileName,
77            string assemblyBackupFile)
78        {
79            if (File.Exists(pdbTempFileName))
80            {
81                File.Copy(pdbTempFileName, pdbFile, true);
82                File.Delete(pdbTempFileName);
83            }
84
85            if (!File.Exists(assemblyBackupFile))
86                return;
87        }
88    }
89}