From: Daniel C. \(kzu\) <dca...@us...> - 2004-10-10 15:28:52
|
Update of /cvsroot/mvp-xml/Design/v1/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10464/v1/src Added Files: ReflectionHelper.cs Log Message: Helper class that provides several reflection-related utilities. --- NEW FILE: ReflectionHelper.cs --- #region using using System; using System.Collections; using System.Diagnostics; using System.IO; using System.Reflection; using System.Security.Permissions; using System.Threading; using System.Web; using System.Globalization; #endregion using namespace Mvp.Xml.Design { /// <summary> /// Provides utility methods for reflection-related operations. /// </summary> public sealed class ReflectionHelper { #region Private ctor // Since this class provides only static methods, make the default constructor private to prevent // instances from being created with "new ReflectionHelper()". private ReflectionHelper() {} #endregion Private ctor #region Miscelaneous /// <summary> /// Ensures that an instance of the type passed as the /// <paramref name="source"/> can be assigned to a variable of /// type <paramref name="assignableTo"/>. /// </summary> /// <param name="source">The type to check.</param> /// <param name="assignableTo">The type the <paramref name="source"/> must be assignable to.</param> public static void EnsureAssignableTo(Type source, Type assignableTo) { if (!assignableTo.IsAssignableFrom(source)) { // TODO: throw new ArgumentException(); // throw new ArgumentException(String.Format( // System.Globalization.CultureInfo.CurrentCulture, // Properties.Resources.ReflectionHelper_CantAssignTo, // source, assignableTo)); } } #endregion Miscelaneous #region Assembly Methods /// <summary> /// Gets the simplified name of the Type, which includes its full namespace qualified /// name and the targetAssembly name, without the other values. /// </summary> /// <param name="type">The type whose name is to be discovered.</param> public static string GetSimpleTypeName(Type type) { if (type == null) throw new ArgumentNullException("type"); return String.Concat(type.FullName, ", ", GetAssemblyName(type)); } /// <summary> /// Gets the simplified name of the object's Type, which includes its full namespace qualified /// name and the targetAssembly name, without the other values. /// </summary> /// <param name="target">The type whose name is to be discovered.</param> public static string GetSimpleTypeName(object target) { if (target == null) throw new ArgumentNullException("target"); return GetSimpleTypeName(target.GetType()); } /// <summary> /// Gets the targetAssembly name, without requiring <see cref="System.Security.Permissions.FileIOPermission"/> on the calling code. /// </summary> /// <param name="targetAssembly">The targetAssembly which name is to be discovered.</param> /// <returns>The same value as "Assembly.GetName().Name".</returns> public static string GetAssemblyName(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); return targetAssembly.FullName.Substring(0, targetAssembly.FullName.IndexOf(",")); } /// <summary> /// Gets the targetAssembly name to which the type belongs, without requiring <see cref="System.Security.Permissions.FileIOPermission"/> on the calling code. /// </summary> /// <param name="type">The type which targetAssembly name is to be discovered.</param> /// <returns>The same value as "Assembly.GetName().Name".</returns> public static string GetAssemblyName(Type type) { if (type == null) throw new ArgumentNullException("type"); return GetAssemblyName(type.Assembly); } /// <summary> /// Returns the targetAssembly part of a fully qualified type name. /// </summary> /// <param name="qualifiedTypeName">Fully qualified type name.</param> /// <returns>The targetAssembly name.</returns> public static string GetAssemblyName(string qualifiedTypeName) { if (qualifiedTypeName == null) throw new ArgumentNullException("qualifiedTypeName"); string[] names = qualifiedTypeName.Split(','); if (names.Length < 2) { // TODO: throw new ArgumentException("qualifiedTypeName"); // throw new ArgumentException(String.Format( // System.Globalization.CultureInfo.CurrentCulture, // Properties.Resources.ReflectionHelper_InvalidQName, qualifiedTypeName)); } return names[1].Trim(); } /// <summary> /// Gets the Assembly Title. /// </summary> /// <param name="targetAssembly">Assembly to get title.</param> /// <returns></returns> public static string GetAssemblyTitle(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); object [] att = targetAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); return ((att.Length > 0) ? ((AssemblyTitleAttribute) att [0]).Title : String.Empty); } /// <summary> /// Instructs a compiler to use a specific version number for the Win32 file version resource. /// The Win32 file version is not required to be the same as the targetAssembly's version number. /// </summary> /// <param name="targetAssembly">Assembly to get version.</param> /// <returns></returns> public static string GetAssemblyFileVersion(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); object[] att = targetAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false); return ((att.Length > 0) ? ((AssemblyFileVersionAttribute) att [0]).Version : String.Empty); } /// <summary> /// Gets the Assembly Product. /// </summary> /// <param name="targetAssembly">Assembly to get product.</param> /// <returns></returns> public static string GetAssemblyProduct(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); object[] att = targetAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false); return ((att.Length > 0) ? ((AssemblyProductAttribute) att [0]).Product : String.Empty); } /// <summary> /// Gets the Assembly Configuration Attribute. /// </summary> /// <param name="targetAssembly">Assembly to get configuration.</param> /// <returns></returns> public static string GetAssemblyConfiguration(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); object[] att = targetAssembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false); return ((att.Length > 0) ? ((AssemblyConfigurationAttribute) att [0]).Configuration : String.Empty); } /// <summary> /// Gets an targetAssembly full path and file name. /// </summary> /// <param name="targetAssembly">Assembly to get path.</param> /// <returns></returns> public static string GetAssemblyPath(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); Uri uri = new Uri(targetAssembly.CodeBase); return uri.LocalPath; } /// <summary> /// Gets an targetAssembly full path and file name. /// </summary> /// <param name="targetAssembly">Assembly to get path.</param> /// <returns></returns> public static string GetAssemblyFolder(Assembly targetAssembly) { if (targetAssembly == null) throw new ArgumentNullException("targetAssembly"); string path = GetAssemblyPath(targetAssembly); path = path.Substring(0, path.LastIndexOf('\\')); return path; } /// <summary> /// Creates the name of a type qualified by the display name of its targetAssembly. /// The format of the returned string is: FullTypeName, AssemblyName. /// </summary> /// <param name="value"></param> /// <returns></returns> public static string CreateShortQualifiedName(Type value) { if (value == null) throw new ArgumentNullException("value"); return Assembly.CreateQualifiedName(GetAssemblyName(value), value.FullName); } #endregion Assembly Methods #region Conventional type loading /// <summary> /// Returns a <see cref="Type"/> from the name received, loaded from the GAC, the base application folder or the calling targetAssembly. /// </summary> /// <param name="className">Fully qualified name of the class to load.</param> /// <returns>The loaded type.</returns> /// <exception cref="TypeLoadException">The type couldn't be loaded.</exception> /// <remarks> /// If the class name is not targetAssembly-qualified, the calling targetAssembly is used to resolve the name /// first. /// </remarks> public static Type LoadType(string className) { if (className == null) throw new ArgumentNullException("className"); return LoadType(className, false); } /// <summary> /// Returns a <see cref="Type"/> from the name received, loaded from the GAC or the base application folder. /// </summary> /// <param name="className">Fully qualified name of the class to load.</param> /// <param name="callingAssembly">True if the type is in the "CallingAssembly".</param> /// <returns>The loaded type.</returns> /// <exception cref="TypeLoadException">The type couldn't be loaded.</exception> public static Type LoadType(string className, bool callingAssembly) { if (className == null) throw new ArgumentNullException("className"); Type reqtype = null; if(className != null && className.Length > 0) { // Method 1 (full class name in the GAC) if(callingAssembly) reqtype = Assembly.GetCallingAssembly().GetType(className.Trim(), false, true); else reqtype = Type.GetType(className.Trim(), false, true); if (reqtype == null) { // Method 2 (partial class name in the GAC) string[] names = className.Split(','); Assembly asm = Assembly.LoadWithPartialName(GetAssemblyName(className)); if (asm == null) { // TODO: throw new TypeLoadException(); // throw new TypeLoadException(String.Format( // System.Globalization.CultureInfo.CurrentCulture, // Properties.Resources.ReflectionHelper_CantLoadAssembly, // names[1], className)); } reqtype = asm.GetType(names[0].Trim(), true, true); } } return reqtype; } #endregion Conventional type loading #region GetAttribute/GetAttributes /// <summary> /// Gets the attribute from the targetAssembly, or <see langword="null"/> if not available. /// </summary> public static Attribute GetAttribute(ICustomAttributeProvider fromAssembly, Type attributeType) { if (fromAssembly == null) throw new ArgumentNullException("fromAssembly"); if (attributeType == null) throw new ArgumentNullException("attributeType"); return GetAttribute(fromAssembly, attributeType, false, null); } /// <summary> /// Gets the attribute from the member (either a type, method, property, etc), or throws the exception received in /// <paramref name="throwIfMissing"/> if not available. /// </summary> public static Attribute GetAttribute(ICustomAttributeProvider fromMember, Type attributeType, Exception throwIfMissing) { if (fromMember == null) throw new ArgumentNullException("fromMember"); if (attributeType == null) throw new ArgumentNullException("attributeType"); return GetAttribute(fromMember, attributeType, false, throwIfMissing); } /// <summary> /// Gets the attribute from the type, or <see langword="null"/> if not available. /// Depending on <paramref name="inherit"/> it will look for base classes too. /// </summary> public static Attribute GetAttribute(ICustomAttributeProvider fromMember, Type attributeType, bool inherit) { if (fromMember == null) throw new ArgumentNullException("fromMember"); if (attributeType == null) throw new ArgumentNullException("attributeType"); return GetAttribute(fromMember, attributeType, inherit, null); } /// <summary> /// Gets the attribute from the member (either a type, method, property, etc), or throws the exception received in /// <paramref name="throwIfMissing"/> if not available. /// Depending on <paramref name="inherit"/> it will look for base classes too. /// </summary> public static Attribute GetAttribute(ICustomAttributeProvider fromMember, Type attributeType, bool inherit, Exception throwIfMissing) { if (fromMember == null) throw new ArgumentNullException("fromMember"); if (attributeType == null) throw new ArgumentNullException("attributeType"); object[] attrs = fromMember.GetCustomAttributes(attributeType, inherit); if (attrs.Length != 0) { return attrs[0] as Attribute; } else { if (throwIfMissing != null) { throw throwIfMissing; } } return null; } #endregion GetAttribute/GetAttributes } } |