此类用于程序集常用反射操作封装
///
/// 内部反射静态类
///
internal static class Reflect
{
///
/// 获取入口程序集
///
///
internal static Assembly GetEntryAssembly()
{
return Assembly.GetEntryAssembly();
}
///
/// 根据程序集名称获取运行时程序集
///
///
///
internal static Assembly GetAssembly(string assemblyName)
{
// 加载程序集
return AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
}
///
/// 根据路径加载程序集
///
///
///
internal static Assembly LoadAssembly(string path)
{
if (!File.Exists(path)) return default;
return Assembly.LoadFrom(path);
}
///
/// 通过流加载程序集
///
///
///
internal static Assembly LoadAssembly(MemoryStream assembly)
{
return Assembly.Load(assembly.ToArray());
}
///
/// 根据程序集名称、类型完整限定名获取运行时类型
///
///
///
///
internal static Type GetType(string assemblyName, string typeFullName)
{
return GetAssembly(assemblyName).GetType(typeFullName);
}
///
/// 根据程序集和类型完全限定名获取运行时类型
///
///
///
///
internal static Type GetType(Assembly assembly, string typeFullName)
{
return assembly.GetType(typeFullName);
}
///
/// 根据程序集和类型完全限定名获取运行时类型
///
///
///
///
internal static Type GetType(MemoryStream assembly, string typeFullName)
{
return LoadAssembly(assembly).GetType(typeFullName);
}
///
/// 获取程序集名称
///
///
///
internal static string GetAssemblyName(Assembly assembly)
{
return assembly.GetName().Name;
}
///
/// 获取程序集名称
///
///
///
internal static string GetAssemblyName(Type type)
{
return GetAssemblyName(type.GetTypeInfo());
}
///
/// 获取程序集名称
///
///
///
internal static string GetAssemblyName(TypeInfo typeInfo)
{
return GetAssemblyName(typeInfo.Assembly);
}
///
/// 加载程序集类型,支持格式:程序集;网站类型命名空间
///
///
///
internal static Type GetStringType(string str)
{
var typeDefinitions = str.Split(";");
return GetType(typeDefinitions[0], typeDefinitions[1]);
}
}