예제 #1
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/>
        /// instance.
        /// </summary>
        /// <param name="typeName">
        /// The (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>
        private Type ResolveType(string typeName)
        {
            #region Sanity Check
            if (typeName == null || typeName.Trim().Length == 0)
            {
                throw BuildTypeLoadException(typeName);
            }
            #endregion

            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type             type     = null;
            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                       LoadTypeDirectlyFromAssembly(typeInfo) :
                       LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception ex)
            {
                throw BuildTypeLoadException(typeName, ex);
            }
            if (type == null)
            {
                throw BuildTypeLoadException(typeName);
            }
            return(type);
        }
예제 #2
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/>
        /// instance.
        /// </summary>
        /// <param name="typeName">
        /// The (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>
        private Type ResolveType(string typeName)
        {
            if (string.IsNullOrEmpty(typeName))
            {
                throw BuildTypeLoadException(typeName);
            }
            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type             type     = null;

            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                       LoadTypeDirectlyFromAssembly(typeInfo) :
                       LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception ex)
            {
                throw BuildTypeLoadException(typeName, ex);
            }
            if (type == null)
            {
                throw BuildTypeLoadException(typeName);
            }
            return(type);
        }
예제 #3
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type     type     = null;
            Assembly assembly = Assembly.Load(typeInfo.AssemblyName);

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
예제 #4
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            // assembly qualified... load the assembly, then the Type
            Assembly assembly = Assembly.Load(typeInfo.AssemblyName);

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
예제 #5
0
 /// <summary>
 /// Check all assembly
 /// to load the attendant <see cref="System.Type"/> referred to by 
 /// the <paramref name="typeInfo"/> parameter.
 /// </summary>
 /// <param name="typeInfo">
 /// The type to be loaded.
 /// </param>
 /// <returns>
 /// A <see cref="System.Type"/>, or <see lang="null"/>.
 /// </returns>
 private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
 {
     Type type = null;
     Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
     foreach (Assembly assembly in assemblies)
     {
         type = assembly.GetType(typeInfo.TypeName, false, false);
         if (type != null)
         {
             break;
         }
     }
     return type;
 }
예제 #6
0
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
        {
            Type type = null;

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                type = assembly.GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    return(type);
                }
            }
            return(type);
        }
예제 #7
0
        /// <summary>
        /// Check all assembly
        /// to load the attendant <see cref="System.Type"/> referred to by
        /// the <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
        {
            Type type = null;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < assemblies.Length; i++)
            {
                type = assemblies[i].GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    break;
                }
            }
            return(type);
        }
예제 #8
0
        /// <summary>
        /// Check all assembly
        /// to load the attendant <see cref="System.Type"/> referred to by
        /// the <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
        {
            Type type = null;

            //获取已加载到此应用程序域的执行上下文中的程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < assemblies.Length; i++)
            {
                //获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项
                type = assemblies[i].GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    break;
                }
            }
            return(type);
        }
예제 #9
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            // assembly qualified... load the assembly, then the Type
            Assembly assembly = null;

            //#if dotnet2
            assembly = Assembly.Load(typeInfo.AssemblyName);
            //#else
            //            assembly = Assembly.LoadWithPartialName (typeInfo.AssemblyName);
            //#endif

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
        /// <summary>
        /// Gets an <b>System.Reflection.Assembly</b> and then the
        /// attendant <b>System.Type</b> referred to by the
        /// <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The assembly to be loaded.
        /// </param>
        /// <returns>
        /// A <b>System.Type</b>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.Load(AssemblyName)"/>
        /// </exception>
        private static Type GetTypeFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type   type         = null;
            string assemblyName = typeInfo.AssemblyName.FullName == Assembly.GetExecutingAssembly().GetName().Name
                        ? typeof(TypeResolver).Assembly.FullName
                        : typeInfo.AssemblyName.FullName;
            AssemblyName an = assemblyName.Contains(",")
                        ? new AssemblyName(assemblyName)
                        : new AssemblyName {
                Name = assemblyName
            };

            Assembly assembly = Assembly.Load(an);

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return(type);
        }
예제 #11
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/>
        /// instance.
        /// </summary>
        /// <param name="typeName">
        /// The (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 static Type ResolveType(string typeName)
        {
            Contract.Require.That(typeName, Is.Not.Null & Is.Not.Empty).When("retrieving argument typeName for ResolveType method");

            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type             type     = null;

            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                       LoadTypeDirectlyFromAssembly(typeInfo) :
                       LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception ex)
            {
                throw BuildTypeLoadException(typeName, ex);
            }

            Contract.Ensure.That <TypeLoadException>(type, Is.Not.Null).When("resolving type name: " + typeName + ". Cause: A type alias for this type is not registered.");

            return(type);
        }
예제 #12
0
        private Type ResolveType(string typeName)
        {
            if ((typeName == null) || (typeName.Trim().Length == 0))
            {
                throw BuildTypeLoadException(typeName);
            }
            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type             type     = null;

            try
            {
                type = typeInfo.IsAssemblyQualified ? LoadTypeDirectlyFromAssembly(typeInfo) : LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception exception)
            {
                throw BuildTypeLoadException(typeName, exception);
            }
            if (type == null)
            {
                throw BuildTypeLoadException(typeName);
            }
            return(type);
        }
        /// <summary>
        /// Gets a <b>System.Type</b> for the <paramref name="typeName"/>
        /// supplied as parameter.
        /// </summary>
        /// <param name="typeName">
        /// The name of a <b>System.Type</b> to resolve.
        /// </param>
        /// <returns>
        /// <b>System.Type</b> instance.
        /// </returns>
        /// <exception cref="System.TypeLoadException">
        /// If the <paramref name="typeName"/> could not be resolved
        /// to a <see cref="System.Type"/>.
        /// </exception>
        public static Type Resolve(string typeName)
        {
            if (string.IsNullOrEmpty(typeName))
            {
                throw CreateTypeLoadException(typeName);
            }

            Type type;
            var  typeInfo = new TypeAssemblyInfo(typeName);

            try
            {
                type = GetTypeFromAssembly(typeInfo);
            }
            catch (Exception ex)
            {
                throw CreateTypeLoadException(typeName, ex);
            }
            if (type == null)
            {
                throw CreateTypeLoadException(typeName);
            }
            return(type);
        }
예제 #14
0
        /// <summary>
        /// Check all assembly
        /// to load the attendant <see cref="System.Type"/> referred to by 
        /// the <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            //获取已加载到此应用程序域的执行上下文中的程序集
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < assemblies.Length; i++)
            {
                //获取程序集实例中具有指定名称的 Type 对象,带有忽略大小写和在找不到该类型时引发异常的选项
                type = assemblies[i].GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    break;
                }
            }
            return type;
        }
예제 #15
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/>
        /// instance.
        /// </summary>
        /// <param name="typeName">
        /// The (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>
        private Type ResolveType(string typeName)
        {
            #region Sanity Check
            if (typeName == null || typeName.Trim().Length == 0)
            {
                throw BuildTypeLoadException(typeName);
            }
            #endregion

            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type type = null;
            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                     LoadTypeDirectlyFromAssembly(typeInfo) :
                     LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception ex)
            {
                throw BuildTypeLoadException(typeName, ex);
            }
            if (type == null)
            {
                throw BuildTypeLoadException(typeName);
            }
            return type;
        }
예제 #16
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            // assembly qualified... load the assembly, then the Type
            Assembly assembly = null;

            #if dotnet2
            assembly = Assembly.Load(typeInfo.AssemblyName);
            #else
            assembly = Assembly.LoadWithPartialName (typeInfo.AssemblyName);
            #endif

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return type;
        }
예제 #17
0
        /// <summary>
        /// Uses <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// to load an <see cref="System.Reflection.Assembly"/> and then the attendant
        /// <see cref="System.Type"/> referred to by the <paramref name="typeInfo"/>
        /// parameter.
        /// </summary>
        /// <remarks>
        /// <p>
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> is
        /// deprecated in .NET 2.0, but is still used here (even when this class is
        /// compiled for .NET 2.0);
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> will
        /// still resolve (non-.NET Framework) local assemblies when given only the
        /// display name of an assembly (the behaviour for .NET Framework assemblies
        /// and strongly named assemblies is documented in the docs for the
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/> method).
        /// </p>
        /// </remarks>
        /// <param name="typeInfo">
        /// The assembly and type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        /// <exception cref="System.Exception">
        /// <see cref="System.Reflection.Assembly.LoadWithPartialName(string)"/>
        /// </exception>
        private static Type LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            // assembly qualified... load the assembly, then the Type
            Assembly assembly = null;

            assembly = Assembly.Load(typeInfo.AssemblyName);

            if (assembly != null)
            {
                type = assembly.GetType(typeInfo.TypeName, true, true);
            }
            return type;
        }
예제 #18
0
        /// <summary>
        /// Check all assembly
        /// to load the attendant <see cref="System.Type"/> referred to by 
        /// the <paramref name="typeInfo"/> parameter.
        /// </summary>
        /// <param name="typeInfo">
        /// The type to be loaded.
        /// </param>
        /// <returns>
        /// A <see cref="System.Type"/>, or <see lang="null"/>.
        /// </returns>
        private static Type LoadTypeByIteratingOverAllLoadedAssemblies(TypeAssemblyInfo typeInfo)
        {
            Type type = null;
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < assemblies.Length; i++)
            {
                type = assemblies[i].GetType(typeInfo.TypeName, false, false);
                if (type != null)
                {
                    break;
                }
            }
            return type;
        }
예제 #19
0
        /// <summary>
        /// Resolves the supplied <paramref name="typeName"/> to a
        /// <see cref="System.Type"/>
        /// instance.
        /// </summary>
        /// <param name="typeName">
        /// The (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 static Type ResolveType(string typeName)
        {
            Contract.Require.That(typeName, Is.Not.Null & Is.Not.Empty).When("retrieving argument typeName for ResolveType method");

            TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName);
            Type type = null;
            try
            {
                type = (typeInfo.IsAssemblyQualified) ?
                     LoadTypeDirectlyFromAssembly(typeInfo) :
                     LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo);
            }
            catch (Exception ex)
            {
                throw BuildTypeLoadException(typeName, ex);
            }

            Contract.Ensure.That<TypeLoadException>(type, Is.Not.Null).When("resolving type name: " + typeName+". Cause: A type alias for this type is not registered.");

            return type;
        }
예제 #20
0
        /// <summary>
        /// 从类型字符串中解析出不含有AssemblyName的类型全名称。
        /// </summary>
        /// <param name="typeName">类型字符串</param>
        /// <returns>类型全名称。</returns>
        public static string GetTypeName(string typeName)
        {
            TypeAssemblyInfo typeAssemblyInfo = new TypeAssemblyInfo(typeName);

            return(typeAssemblyInfo.TypeName);
        }