You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

137 lines
4.8 KiB

using Microsoft.Extensions.DependencyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Znyc.Cloudcar.Admin.Commons.Options;
namespace Znyc.Cloudcar.Admin.Commons.Helpers
{
/// <summary>
/// </summary>
public class RuntimeHelper
{
/// <summary>
/// 获取项目程序集,排除所有的系统程序集(Microsoft.***、System.***等)、Nuget下载包
/// </summary>
/// <returns></returns>
public static IList<Assembly> GetAllAssemblies()
{
List<Assembly> list = new List<Assembly>();
DependencyContext deps = DependencyContext.Default;
//排除所有的系统程序集、Nuget下载包
IEnumerable<CompilationLibrary> libs = deps.CompileLibraries.Where(lib => lib.Type == AssembleTypeConsts.Project); //只获取本项目用到的包
foreach (CompilationLibrary lib in libs)
{
try
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
list.Add(assembly);
}
catch (Exception)
{
// ignored
}
}
return list;
}
/// <summary>
/// 获取项目程序集,排除所有的系统程序集(Microsoft.***、System.***等)、Nuget下载包和Znyc.Cloudcar.Admin.Commons.dll
/// 获取所有关于Znyc的程序集
/// </summary>
/// <returns></returns>
public static IList<Assembly> GetAllZnycAssemblies()
{
List<Assembly> list = new List<Assembly>();
DependencyContext deps = DependencyContext.Default;
//排除所有的系统程序集、Nuget下载包
IEnumerable<CompilationLibrary> libs = deps.CompileLibraries.Where(lib =>
lib.Type == AssembleTypeConsts.Project || lib.Name.StartsWith("Znyc")); //只获取本项目用到的包
foreach (CompilationLibrary lib in libs)
{
try
{
if (lib.Name != "Znyc.Cloudcar.Admin.Commons")
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
list.Add(assembly);
}
}
catch (Exception)
{
// ignored
}
}
return list;
}
/// <summary>
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
public static Assembly GetAssembly(string assemblyName)
{
return GetAllZnycAssemblies().FirstOrDefault(assembly => assembly.FullName.Contains(assemblyName));
}
/// <summary>
/// </summary>
/// <returns></returns>
public static IList<Type> GetAllTypes()
{
List<Type> list = new List<Type>();
foreach (Assembly assembly in GetAllAssemblies())
{
IEnumerable<TypeInfo> typeInfos = assembly.DefinedTypes;
foreach (TypeInfo typeInfo in typeInfos)
{
list.Add(typeInfo.AsType());
}
}
return list;
}
/// <summary>
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
public static IList<Type> GetTypesByAssembly(string assemblyName)
{
List<Type> list = new List<Type>();
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));
IEnumerable<TypeInfo> typeInfos = assembly.DefinedTypes;
foreach (TypeInfo typeInfo in typeInfos)
{
list.Add(typeInfo.AsType());
}
return list;
}
/// <summary>
/// 获取实现类
/// </summary>
/// <param name="typeName"></param>
/// <param name="baseInterfaceType"></param>
/// <returns></returns>
public static Type GetImplementType(string typeName, Type baseInterfaceType)
{
return GetAllTypes().FirstOrDefault(t =>
{
if (t.Name == typeName &&
t.GetTypeInfo().GetInterfaces().Any(b => b.Name == baseInterfaceType.Name))
{
TypeInfo typeInfo = t.GetTypeInfo();
return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType;
}
return false;
});
}
}
}