using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace NBF { public static class Reflection { private static readonly Assembly _assembly; public static Assembly Assembly => _assembly; static Reflection() { _assembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Assembly-CSharp"); Types = _assembly.GetTypes(); } public static Type[] Types { get; private set; } /// /// 获取所有非抽象的派生类 /// /// /// public static List GetAllNonAbstractDerivedTypes() { Type baseType = typeof(T); Assembly assembly = Assembly.GetAssembly(baseType); List derivedTypes = assembly.GetTypes() .Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(baseType)) .ToList(); return derivedTypes; } /// /// 获取所有标记指定特性的方法 /// /// 查找类 /// 特性类 /// public static List GetMethodsWithUIInputAttribute(Type classType, Type attributeType) { List methodsWithAttribute = new List(); if (classType == null) { return methodsWithAttribute; } // 获取所有方法,包括公共、非公共、实例和静态方法 MethodInfo[] allMethods = classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo method in allMethods) { if (method.GetCustomAttributes(attributeType, false).Length > 0) { methodsWithAttribute.Add(method); } } return methodsWithAttribute; } /// /// 获取所有标记指定特性的方法 /// /// 查找类 /// public static Dictionary GetMethodsAttribute(Type classType) where T : Attribute { Dictionary methodsWithAttribute = new Dictionary(); if (classType == null) { return methodsWithAttribute; } // 获取所有方法,包括公共、非公共、实例和静态方法 MethodInfo[] allMethods = classType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo method in allMethods) { var attribute = method.GetCustomAttribute(); if (attribute != null) { methodsWithAttribute[method] = attribute; } } return methodsWithAttribute; } } }