Exemple #1
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/> instance.
        /// </summary>
        /// <param name="typeName">
        /// The unresolved (possibly partially assembly qualified) name 
        /// of a <see cref="System.Type"/>.
        /// </param>
        /// <returns>
        /// A resolved <see cref="System.Type"/> instance.
        /// </returns>
        /// <exception cref="System.TypeLoadException">
        /// If the supplied <paramref name="typeName"/> could not be resolved
        /// to a <see cref="System.Type"/>.
        /// </exception>
        public virtual Type Resolve(string typeName)
        {
            if (string.IsNullOrEmpty(typeName))
                throw BuildTypeLoadException(typeName);

            var typeInfo = new TypeAssemblyHolder(typeName);
            Type type;
            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                     LoadTypeDirectlyFromAssembly(typeInfo) :
                     LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch
            {
                try
                {
                    type = LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
                }
                catch (Exception ex)
                {
                    if (ex is TypeLoadException)
                        throw;

                    throw BuildTypeLoadException(typeName, ex);
                }
            }
            
            return type;
        }
 public void CanTakeUnqualifiedType()
 {
     var testType = typeof (Category);
     var typeAssemblyHolder = new TypeAssemblyHolder(testType.FullName);
     Assert.IsFalse(typeAssemblyHolder.IsAssemblyQualified);
     Assert.AreEqual(testType.FullName, typeAssemblyHolder.GetTypeName());
     Assert.AreEqual(null, typeAssemblyHolder.GetAssemblyName());
 }
 public void CanTakeUnqualifiedGenericType()
 {
     var testType = typeof (TestGenericObject<int, string>);
     var tah = new TypeAssemblyHolder(testType.FullName);
     Assert.IsFalse(tah.IsAssemblyQualified);
     Assert.AreEqual(testType.FullName, tah.GetTypeName());
     Assert.AreEqual(null, tah.GetAssemblyName());
 }
Exemple #4
0
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyHolder typeInfo)
        {
            Type type = null;

            var assembly = Assembly.Load(typeInfo.GetAssemblyName());

            if (assembly != null)
                type = assembly.GetType(typeInfo.GetTypeName(), true, true);

            return type;
        }
Exemple #5
0
 private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyHolder typeInfo)
 {
     Type type = null;
     var assemblies = AppDomain.CurrentDomain.GetAssemblies();
     foreach (var assembly in assemblies)
     {
         type = assembly.GetType(typeInfo.GetTypeName(), false, false);
         if (type != null)
             break;
     }
     return type;
 }
Exemple #6
0
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyHolder typeInfo)
        {
            Type type = null;

            var assembly = Assembly.Load(typeInfo.GetAssemblyName());

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.GetTypeName(), true, true);
            }

            return(type);
        }
Exemple #7
0
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyHolder typeInfo)
        {
            Type type       = null;
            var  assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                type = assembly.GetType(typeInfo.GetTypeName(), false, false);
                if (type != null)
                {
                    break;
                }
            }
            return(type);
        }