PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/AssemblyResolver.cs

#
C# | 124 lines | 97 code | 18 blank | 9 comment | 9 complexity | 1c9302ce8f2d400069d00e6be3c334c0 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. namespace NUnit.Core
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Collections;
  12. /// <summary>
  13. /// Class adapted from NUnitAddin for use in handling assemblies that are not
  14. /// found in the test AppDomain.
  15. /// </summary>
  16. public class AssemblyResolver : MarshalByRefObject, IDisposable
  17. {
  18. static Logger log = InternalTrace.GetLogger(typeof(AssemblyResolver));
  19. private class AssemblyCache
  20. {
  21. private Hashtable _resolved = new Hashtable();
  22. public bool Contains( string name )
  23. {
  24. return _resolved.ContainsKey( name );
  25. }
  26. public Assembly Resolve( string name )
  27. {
  28. if ( _resolved.ContainsKey( name ) )
  29. return (Assembly)_resolved[name];
  30. return null;
  31. }
  32. public void Add( string name, Assembly assembly )
  33. {
  34. _resolved[name] = assembly;
  35. }
  36. }
  37. private AssemblyCache _cache = new AssemblyCache();
  38. private ArrayList _dirs = new ArrayList();
  39. public AssemblyResolver()
  40. {
  41. AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  42. }
  43. public void Dispose()
  44. {
  45. AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  46. }
  47. public void AddFile( string file )
  48. {
  49. Assembly assembly = Assembly.LoadFrom( file );
  50. _cache.Add(assembly.GetName().FullName, assembly);
  51. }
  52. public void AddFiles( string directory, string pattern )
  53. {
  54. if ( Directory.Exists( directory ) )
  55. foreach( string file in Directory.GetFiles( directory, pattern ) )
  56. AddFile( file );
  57. }
  58. public void AddDirectory( string directory )
  59. {
  60. if ( Directory.Exists( directory ) )
  61. _dirs.Add( directory );
  62. }
  63. private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  64. {
  65. string fullName = args.Name;
  66. int index = fullName.IndexOf(',');
  67. if(index == -1) // Only resolve using full name.
  68. {
  69. log.Debug( string.Format("Not a strong name: {0}", fullName ) );
  70. return null;
  71. }
  72. if ( _cache.Contains( fullName ) )
  73. {
  74. log.Info( string.Format( "Resolved from Cache: {0}", fullName ) );
  75. return _cache.Resolve(fullName);
  76. }
  77. foreach( string dir in _dirs )
  78. {
  79. foreach( string file in Directory.GetFiles( dir, "*.dll" ) )
  80. {
  81. string fullFile = Path.Combine( dir, file );
  82. AssemblyReader rdr = new AssemblyReader(fullFile);
  83. try
  84. {
  85. if (rdr.IsDotNetFile)
  86. {
  87. if (AssemblyName.GetAssemblyName(fullFile).FullName == fullName)
  88. {
  89. log.Info(string.Format("Added to Cache: {0}", fullFile));
  90. AddFile(fullFile);
  91. return _cache.Resolve(fullName);
  92. }
  93. }
  94. }
  95. catch(Exception ex)
  96. {
  97. log.Error( "Unable to load addin assembly", ex );
  98. throw;
  99. }
  100. }
  101. }
  102. log.Debug( string.Format( "Not in Cache: {0}", fullName) );
  103. return null;
  104. }
  105. }
  106. }