1 // ***********************************************************************
2 // Copyright (c) 2008 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 using System.Reflection;
34 namespace NUnit.Engine.Internal
36 namespace NUnit.Framework.Internal
38 namespace NUnit.Common
42 /// AssemblyHelper provides static methods for working
45 public class AssemblyHelper
47 #region GetAssemblyPath
50 /// Gets the path from which the assembly defining a type was loaded.
52 /// <param name="type">The Type.</param>
53 /// <returns>The path.</returns>
54 public static string GetAssemblyPath(Type type)
57 return GetAssemblyPath(type.GetTypeInfo().Assembly);
59 return GetAssemblyPath(type.Assembly);
64 /// Gets the path from which an assembly was loaded.
65 /// For builds where this is not possible, returns
66 /// the name of the assembly.
68 /// <param name="assembly">The assembly.</param>
69 /// <returns>The path.</returns>
70 public static string GetAssemblyPath(Assembly assembly)
73 return GetAssemblyName(assembly).Name;
74 #elif NETCF || PORTABLE
75 return assembly.ManifestModule.FullyQualifiedName;
77 string codeBase = assembly.CodeBase;
79 if (IsFileUri(codeBase))
80 return GetAssemblyPathFromCodeBase(codeBase);
82 return assembly.Location;
88 #region GetDirectoryName
90 #if !SILVERLIGHT && !PORTABLE
92 /// Gets the path to the directory from which an assembly was loaded.
94 /// <param name="assembly">The assembly.</param>
95 /// <returns>The path.</returns>
96 public static string GetDirectoryName(Assembly assembly)
98 return Path.GetDirectoryName(GetAssemblyPath(assembly));
104 #region GetAssemblyName
107 /// Gets the AssemblyName of an assembly.
109 /// <param name="assembly">The assembly</param>
110 /// <returns>An AssemblyName</returns>
111 public static AssemblyName GetAssemblyName(Assembly assembly)
113 #if SILVERLIGHT || PORTABLE
114 return new AssemblyName(assembly.FullName);
116 return assembly.GetName();
126 /// Loads an assembly given a string, which is the AssemblyName
128 /// <param name="name"></param>
129 /// <returns></returns>
130 public static Assembly Load(string name)
132 var ext = Path.GetExtension(name);
133 if (ext == ".dll" || ext == ".exe")
134 name = Path.GetFileNameWithoutExtension(name);
136 return Assembly.Load(new AssemblyName { Name = name });
141 /// Loads an assembly given a string, which may be the
142 /// path to the assembly or the AssemblyName
144 /// <param name="nameOrPath"></param>
145 /// <returns></returns>
146 public static Assembly Load(string nameOrPath)
148 var ext = Path.GetExtension(nameOrPath).ToLower();
150 // Handle case where this is the path to an assembly
151 if (ext == ".dll" || ext == ".exe")
154 return Assembly.Load(Path.GetFileNameWithoutExtension(nameOrPath));
156 return Assembly.LoadFrom(nameOrPath);
158 return Assembly.Load(AssemblyName.GetAssemblyName(nameOrPath));
162 // Assume it's the string representation of an AssemblyName
163 return Assembly.Load(nameOrPath);
169 #region Helper Methods
171 #if !NETCF && !SILVERLIGHT && !PORTABLE
172 private static bool IsFileUri(string uri)
174 return uri.ToLower().StartsWith(Uri.UriSchemeFile);
178 /// Gets the assembly path from code base.
180 /// <remarks>Public for testing purposes</remarks>
181 /// <param name="codeBase">The code base.</param>
182 /// <returns></returns>
183 public static string GetAssemblyPathFromCodeBase(string codeBase)
185 // Skip over the file:// part
186 int start = Uri.UriSchemeFile.Length + Uri.SchemeDelimiter.Length;
188 if (codeBase[start] == '/') // third slash means a local path
190 // Handle Windows Drive specifications
191 if (codeBase[start + 2] == ':')
193 // else leave the last slash so path is absolute
195 else // It's either a Windows Drive spec or a share
197 if (codeBase[start + 1] != ':')
198 start -= 2; // Back up to include two slashes
201 return codeBase.Substring(start);