Exemplo n.º 1
0
        /// <summary>
        /// 根据类型描述得到类型对象
        /// </summary>
        /// <param name="typeDescription">完整类型描述,应该是Namespace.ClassName, AssemblyName</param>
        /// <returns>类型对象</returns>
        public static Type GetTypeInfo(string typeDescription)
        {
            ExceptionHelper.CheckStringIsNullOrEmpty(typeDescription, "typeDescription");

            Type result = Type.GetType(typeDescription);

            if (result == null)
            {
                TypeInfo ti = GenerateTypeInfo(typeDescription);

                AssemblyName aName = new AssemblyName(ti.AssemblyName);

                AssemblyMappingConfigurationElement element = AssemblyMappingSettings.GetConfig().Mapping[aName.Name];

                ExceptionHelper.TrueThrow <TypeLoadException>(element == null, "不能找到类型{0}", typeDescription);

                ti.AssemblyName = element.MapTo;

                result = Type.GetType(ti.ToString());

                ExceptionHelper.FalseThrow <TypeLoadException>(result != null, "不能得到类型信息{0}", ti.ToString());
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据类型描述试图得到类型对象,如果成功,返回true,否则为false
        /// </summary>
        /// <param name="typeDescription"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool TryGetTypeInfo(string typeDescription, out Type type)
        {
            type = Type.GetType(typeDescription);

            if (type == null)
            {
                TypeInfo ti = GenerateTypeInfo(typeDescription);

                AssemblyName aName = new AssemblyName(ti.AssemblyName);

                AssemblyMappingConfigurationElement element = AssemblyMappingSettings.GetConfig().Mapping[aName.Name];

                if (element != null)
                {
                    ti.AssemblyName = element.MapTo;
                    type            = Type.GetType(ti.ToString());
                }
            }

            return(type != null);
        }