PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.Reflection.Emit/AssemblyDefinitionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 58 lines | 30 code | 5 blank | 23 comment | 0 complexity | d84efa0b17d58fedb75ea5c94d2548d6 MD5 | raw file
  1. using System.IO;
  2. using System.Reflection;
  3. using Mono.Cecil;
  4. namespace LinFu.Reflection.Emit
  5. {
  6. /// <summary>
  7. /// A class that adds extension methods to the <see cref="AssemblyDefinition" />
  8. /// class.
  9. /// </summary>
  10. public static class AssemblyDefinitionExtensions
  11. {
  12. /// <summary>
  13. /// Converts an <see cref="AssemblyDefinition" />
  14. /// into a running <see cref="Assembly" />.
  15. /// </summary>
  16. /// <param name="definition">The <see cref="AssemblyDefinition" /> to convert.</param>
  17. /// <returns>
  18. /// An <see cref="Assembly" /> that represents the <see cref="AssemblyDefinition" /> instance.
  19. /// </returns>
  20. public static Assembly ToAssembly(this AssemblyDefinition definition)
  21. {
  22. Assembly result = null;
  23. using (var stream = new MemoryStream())
  24. {
  25. // Persist the assembly to the stream
  26. definition.Save(stream);
  27. var buffer = stream.GetBuffer();
  28. result = Assembly.Load(buffer);
  29. }
  30. return result;
  31. }
  32. /// <summary>
  33. /// Saves the assembly to disk.
  34. /// </summary>
  35. /// <param name="definition">The target assembly definition.</param>
  36. /// <param name="filename">The output file name.</param>
  37. public static void Save(this AssemblyDefinition definition, string filename)
  38. {
  39. var memoryStream = new MemoryStream();
  40. definition.Save(memoryStream);
  41. File.WriteAllBytes(filename, memoryStream.ToArray());
  42. }
  43. /// <summary>
  44. /// Saves the assembly to disk.
  45. /// </summary>
  46. /// <param name="definition">The target assembly definition.</param>
  47. /// <param name="outputStream">The destination file stream.</param>
  48. public static void Save(this AssemblyDefinition definition, Stream outputStream)
  49. {
  50. definition.Write(outputStream);
  51. }
  52. }
  53. }