using System.Reflection;
namespace MasstransferCommon.Utils;
///
/// 包含程序集相关的工具类
///
public class AssemblyUtil
{
private const string Token = "masstransfer";
///
/// 根据注解来获取类信息
///
///
///
public static List GetTypesByAttribute(Type attributeType)
{
return GetAssemblies()
.SelectMany(a => a.GetTypes()
.Where(t => t.GetCustomAttributes(attributeType, true).Length > 0))
.ToList();
}
private static IEnumerable GetAssemblies()
{
var assemblies = new List();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var name = assembly.GetName().Name;
if (name != null && name.ToLower().Contains(Token)) GetReferenceAssemblies(assembly, assemblies);
}
return assemblies;
}
private static void GetReferenceAssemblies(Assembly assembly, ICollection assemblies)
{
foreach (var assemblyName in assembly.GetReferencedAssemblies())
{
var name = assemblyName.Name;
if (name == null || !name.ToLower().Contains(Token)) continue;
var ass = Assembly.Load(assemblyName);
if (assemblies.Contains(ass)) continue;
assemblies.Add(ass);
GetReferenceAssemblies(ass, assemblies);
}
}
}