|
// System Using Statements
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security;
namespace CommonUtilities
{
/// <summary>
/// Helper class for handling .NET Assembly objects
/// </summary>
public class AssemblyHelper
{
/// <summary>
/// Returns the assembly file name
/// </summary>
/// <param name="myAssembly">the assembly which file name is required</param>
/// <returns>a string that represents the assembly file name</returns>
public static string AssemblyFileName(Assembly myAssembly) {
string fileName = myAssembly.CodeBase;
if (fileName != null)
{
Uri uri = new Uri(fileName);
fileName = uri.LocalPath;
}
return fileName;
}
/// <summary>
/// Returns the location information of an assembly
/// </summary>
/// <param name="myAssembly">the assembyl</param>
/// <returns>a string that represents the assembly location</returns>
internal static string AssemblyLocationInfo(Assembly myAssembly) {
string text1;
if (myAssembly.GlobalAssemblyCache) {
text1 = "Global Assembly Cache";
}
else {
try {
text1 = myAssembly.Location;
}
catch (SecurityException) {
text1 = "Location Permission Denied";
}
}
return text1;
}
/// <summary>
/// Returns the assembly qualified name for a given type
/// </summary>
/// <param name="type">the type</param>
/// <returns>the qualified name of the assembly</returns>
public static string AssemblyQualifiedName(Type type) {
string text1;
text1 = string.Concat(type.FullName, ", ", type.Assembly.FullName);
return text1;
}
/// <summary>
/// Returns the assembly short name
/// </summary>
/// <param name="myAssembly">the assembly</param>
/// <returns>the assembly short name</returns>
public static string AssemblyShortName(Assembly myAssembly) {
string longName;
int comaIndex;
string shortName;
longName = myAssembly.FullName;
comaIndex = longName.IndexOf(',');
if (comaIndex > 0) {
longName = longName.Substring(0, comaIndex);
}
shortName = longName.Trim();
return shortName;
}
/// <summary>
/// Get a type by name in an assembly
/// </summary>
/// <param name="relativeAssembly">the assembly to search for the type</param>
/// <param name="typeName">the type name to search</param>
/// <param name="throwOnError">throw exception if not found</param>
/// <returns>the type</returns>
public static Type GetTypeFromString(Assembly relativeAssembly, string typeName, bool throwOnError) {
Type foundType;
if (typeName.IndexOf(',') == -1) {
foundType = relativeAssembly.GetType(typeName, throwOnError);
}
else {
foundType = Type.GetType(typeName, throwOnError);
}
return foundType;
}
/// <summary>
/// cache the loaded assemblies
/// </summary>
private static readonly Dictionary<string, Assembly> cachedAssemblies = new Dictionary<string, Assembly>();
/// <summary>
/// Gets loaded assembly.
/// in case assembly isn't loaded, returns null.
/// </summary>
/// <param name="assemblyName">the assembly name to get</param>
/// <returns>the loaded assembly or null if not found</returns>
public static Assembly GetLoadedAssmebly(string assemblyName)
{
Assembly retAsm;
if (cachedAssemblies.TryGetValue(assemblyName.ToLower() , out retAsm)) return retAsm;
// get the current domain
AppDomain currentDomain = AppDomain.CurrentDomain;
// check if the assembly was loaded before
Assembly[] assemblies =currentDomain.GetAssemblies();
foreach(Assembly asm in assemblies) {
AssemblyName asmName = asm.GetName();
if (asmName.Name != null)
{
// cache it
cachedAssemblies[asmName.Name.ToLower()] = asm;
}
if (string.Compare(asmName.Name, assemblyName, true) == 0)
return asm;
}
// no found
return null;
}
/// <summary>
/// Loads assembly
/// first check if the assembly was loaded otherwise load it
/// </summary>
/// <param name="assemblyName">the assembly name to load</param>
/// <returns>the loaded assembly or null if not found</returns>
public static Assembly LoadAssembly(string assemblyName) {
// first, assume the assembly is already loaded.
Assembly assembly = GetLoadedAssmebly(assemblyName);
// if not, load it
if (assembly != null) return assembly;
assembly = AppDomain.CurrentDomain.Load(assemblyName);
if (assembly != null)
{
AssemblyName asmName = assembly.GetName();
if (asmName.Name != null)
{
// cache it
cachedAssemblies[asmName.Name.ToLower()] = assembly;
}
}
return assembly;
}
/// <summary>
/// Get type from string
/// </summary>
/// <param name="typeName">the type name</param>
/// <param name="throwOnError">throw exception if not found</param>
/// <returns>the type</returns>
public static Type GetTypeFromString(string typeName, bool throwOnError) {
Type foundType;
foundType = AssemblyHelper.GetTypeFromString(Assembly.GetCallingAssembly(), typeName, throwOnError);
return foundType;
}
/// <summary>
/// Get type from string using a relative type which exists at the same assembly
/// </summary>
/// <param name="relativeType">the relative type</param>
/// <param name="typeName">the type name</param>
/// <param name="throwOnError">throw exception if not found</param>
/// <returns>the type requested</returns>
public static Type GetTypeFromString(Type relativeType, string typeName, bool throwOnError) {
Type foundType;
foundType = AssemblyHelper.GetTypeFromString(relativeType.Assembly, typeName, throwOnError);
return foundType;
}
/// <summary>
/// The application base directory
/// </summary>
public static string ApplicationBaseDirectory {
get {
string baseDirectory;
baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
return baseDirectory;
}
}
/// <summary>
/// Gets the configuration file location
/// </summary>
public static string ConfigurationFileLocation {
get {
string configurationFile;
configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
return configurationFile;
}
}
/// <summary>
/// The entry assembly location
/// </summary>
public static string EntryAssemblyLocation() {
string location;
location = Assembly.GetEntryAssembly().Location;
return location;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Project Manager and Application Developer, in a wide variety of business applications. Particularly interested in client/server and Graphical User Interface design using Visual C#.
Specialties: 13 Y'rs in C#, 10 Y'rs experience in C++ Highly experienced in wide technologies, IT projects, military projects etc'.