1 // ****************************************************************
\r
2 // This is free software licensed under the NUnit license. You
\r
3 // may obtain a copy of the license as well as information regarding
\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
\r
5 // ****************************************************************
\r
11 using System.Reflection;
\r
12 using System.Collections;
\r
15 /// Class adapted from NUnitAddin for use in handling assemblies that are not
\r
16 /// found in the test AppDomain.
\r
18 public class AssemblyResolver : MarshalByRefObject, IDisposable
\r
20 private class AssemblyCache
\r
22 private Hashtable _resolved = new Hashtable();
\r
24 public bool Contains( string name )
\r
26 return _resolved.ContainsKey( name );
\r
29 public Assembly Resolve( string name )
\r
31 if ( _resolved.ContainsKey( name ) )
\r
32 return (Assembly)_resolved[name];
\r
37 public void Add( string name, Assembly assembly )
\r
39 _resolved[name] = assembly;
\r
43 private AssemblyCache _cache = new AssemblyCache();
\r
45 private ArrayList _dirs = new ArrayList();
\r
47 public AssemblyResolver()
\r
49 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
\r
52 public void Dispose()
\r
54 AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
\r
57 public void AddFile( string file )
\r
59 Assembly assembly = Assembly.LoadFrom( file );
\r
60 _cache.Add(assembly.GetName().FullName, assembly);
\r
63 public void AddFiles( string directory, string pattern )
\r
65 if ( Directory.Exists( directory ) )
\r
66 foreach( string file in Directory.GetFiles( directory, pattern ) )
\r
70 public void AddDirectory( string directory )
\r
72 if ( Directory.Exists( directory ) )
\r
73 _dirs.Add( directory );
\r
76 private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
\r
78 string fullName = args.Name;
\r
79 int index = fullName.IndexOf(',');
\r
80 if(index == -1) // Only resolve using full name.
\r
82 NTrace.Debug( string.Format("Not a strong name: {0}", fullName ),
\r
83 "'AssemblyResolver'" );
\r
87 if ( _cache.Contains( fullName ) )
\r
89 NTrace.Info( string.Format( "Resolved from Cache: {0}", fullName ),
\r
90 "'AssemblyResolver'" );
\r
91 return _cache.Resolve(fullName);
\r
94 foreach( string dir in _dirs )
\r
96 foreach( string file in Directory.GetFiles( dir, "*.dll" ) )
\r
98 string fullFile = Path.Combine( dir, file );
\r
101 if ( AssemblyName.GetAssemblyName( fullFile ).FullName == fullName )
\r
103 NTrace.Info( string.Format( "Added to Cache: {0}", fullFile ),
\r
104 "'AssemblyResolver'" );
\r
105 AddFile( fullFile );
\r
106 return _cache.Resolve( fullName );
\r
111 // Keep going if there's a bad assembly
\r
112 NTrace.Debug( string.Format( "Bad assembly: {0}", fullFile ), "AssemblyResolver");
\r
117 NTrace.Debug( string.Format( "Not in Cache: {0}", fullName),
\r
118 "'AssemblyResolver'");
\r