/// <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) { Check.Argument.IsNotEmpty("typeName", typeName); TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName); Type type = null; try { type = (typeInfo.IsAssemblyQualified) ? LoadTypeDirectlyFromAssembly(typeInfo) : LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo); } catch (Exception ex) { ThrowHelper.ThrowTypeLoadExcetpion(ex, R.TypeLoadFail, typeName); } if (type == null) { ThrowHelper.ThrowTypeLoadExcetpion(R.TypeLoadFail, typeName); } return(type); }
/// <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); }
/// <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); }
/// <summary> /// 从类型字符串中解析出不含有AssemblyName的类型全名称。 /// </summary> /// <param name="typeName">类型字符串</param> /// <returns>类型全名称。</returns> public static string GetTypeName(string typeName) { TypeAssemblyInfo typeAssemblyInfo = new TypeAssemblyInfo(typeName); return typeAssemblyInfo.TypeName; }
/// <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; }
/// <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; }
/// <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) { Check.Argument.IsNotEmpty("typeName", typeName); TypeAssemblyInfo typeInfo = new TypeAssemblyInfo(typeName); Type type = null; try { type = (typeInfo.IsAssemblyQualified) ? LoadTypeDirectlyFromAssembly(typeInfo) : LoadTypeByIteratingOverAllLoadedAssemblies(typeInfo); } catch (Exception ex) { ThrowHelper.ThrowTypeLoadExcetpion(ex, R.TypeLoadFail, typeName); } if (type == null) { ThrowHelper.ThrowTypeLoadExcetpion(R.TypeLoadFail, typeName); } return type; }