From: Michael D. <mik...@us...> - 2005-04-05 14:21:33
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8929/NHibernate/Util Modified Files: ReflectHelper.cs Log Message: NH-223: added method to load a type from an assembly with its partial name. Index: ReflectHelper.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Util/ReflectHelper.cs,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** ReflectHelper.cs 26 Mar 2005 13:24:37 -0000 1.25 --- ReflectHelper.cs 5 Apr 2005 14:21:21 -0000 1.26 *************** *** 12,15 **** --- 12,17 ---- public sealed class ReflectHelper { + private static readonly log4net.ILog log = log4net.LogManager.GetLogger( typeof( ReflectHelper ) ); + private ReflectHelper() { *************** *** 155,158 **** --- 157,207 ---- /// <summary> + /// Returns a <see cref="System.Type"/> from an already loaded Assembly or an + /// Assembly that is loaded with a partial name. + /// </summary> + /// <param name="className">The full name of the class.</param> + /// <param name="assemblyName"> + /// The name of the assembly. This can be the full assembly name or just the partial name. + /// </param> + /// <returns> + /// The <see cref="System.Type"/> for the class in the assembly or + /// <c>null</c> if a <see cref="System.Type"/> can't be found. + /// </returns> + /// <remarks> + /// Attempts to get a reference to the type from an already loaded assembly. If the + /// Type can be found then the Assembly is loaded using LoadWithPartialName. + /// </remarks> + public static System.Type TypeFromAssembly( string className, string assemblyName ) + { + try + { + // try to get the Types from an already loaded assembly + System.Type type = System.Type.GetType( className + ", " + assemblyName ); + + // if the type is null then the assembly is not loaded. + if( type==null ) + { + // use the partial name because we don't know the public key, version, culture-info of + // the assembly on the local machine. + Assembly assembly = Assembly.LoadWithPartialName( assemblyName ); + if( assembly!=null ) + { + type = assembly.GetType( className ); + } + } + + return type; + } + catch( Exception e ) + { + if( log.IsErrorEnabled ) + { + log.Error( className + ", " + assemblyName + " could not be loaded.", e ); + } + return null; + } + } + + /// <summary> /// Returns the value contained in the static field. /// </summary> |