/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

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