using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.IO; using CommonLang.Log; namespace CommonLang.Property { public class PropertyList : List where T : new() { public PropertyList() { } public PropertyList(T[] other) { AddRange(other); } public PropertyList(ICollection other) { AddRange(other); } } public class TypeComparer : IComparer { public int Compare(Type x, Type y) { return x.FullName.CompareTo(y.FullName); } } public class ToStringComparer : IComparer { public int Compare(T x, T y) { return x.ToString().CompareTo(y.ToString()); } } public static class ReflectionUtil { public static void LoadDlls(DirectoryInfo dirinfo) { AppDomain domain = AppDomain.CurrentDomain; foreach (FileInfo file in dirinfo.GetFiles()) { if (file.Extension.ToLower().Equals(".dll")) { try { Assembly asm = Assembly.ReflectionOnlyLoadFrom(file.FullName); if (!ExistAssembly(domain, asm.FullName)) { domain.Load(asm.FullName); } } catch (Exception err) { Console.WriteLine(err.Message); } } } } private static bool ExistAssembly(AppDomain domain, string name) { Assembly[] asms = domain.GetAssemblies(); foreach (Assembly asm in asms) { if (asm.FullName.Equals(name)) { return true; } } return false; } public static Type LoadTypeFromDLL(string path) { Assembly asm = Assembly.ReflectionOnlyLoadFrom(path); asm = AppDomain.CurrentDomain.Load(asm.FullName); Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T)); return data_plugin_type; } public static T LoadInterfaceFromDLL(string path) { Assembly asm = Assembly.ReflectionOnlyLoadFrom(path); asm = AppDomain.CurrentDomain.Load(asm.FullName); Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T)); T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type); return formula; } public static T LoadInterfaceFromAssemblyName(string assembly_name) { Assembly asm = AppDomain.CurrentDomain.Load(assembly_name); Type data_plugin_type = ReflectionUtil.FindTypeFromAssembly(asm, typeof(T)); T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type); return formula; } public static Type FindTypeFromAssembly(Assembly assembly, Type baseType) { foreach (Type type in assembly.GetTypes()) { if (baseType.IsAssignableFrom(type)) { return type; } } return null; } public static object CreateInstance(Type valueType, params object[] args) { try { if (valueType.IsArray) { int[] lengths = new int[valueType.GetArrayRank()]; for (int i = 0; i < lengths.Length; i++) { if (i < args.Length) { lengths[i] = (int)args[i]; } else { lengths[i] = 0; } } var elementType = valueType.GetElementType(); return Array.CreateInstance(elementType, lengths); } else { return Activator.CreateInstance(valueType, args); } } catch (Exception err) { throw new Exception(valueType.FullName, err); } } public static T CreateInterface(string type, params object[] args) { Type data_plugin_type = ReflectionUtil.GetType(type); T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type, args); return formula; } public static T CreateInterface(Type data_plugin_type, params object[] args) { T formula = (T)ReflectionUtil.CreateInstance(data_plugin_type, args); return formula; } public static object CreateInstance(Type valueType) { return CreateInstance(valueType, new object[0]); } public static T CreateInterface(string type) { return CreateInterface(type, new object[0]); } public static T CreateInterface(Type data_plugin_type) { return CreateInterface(data_plugin_type, new object[0]); } public static Type GetType(string vtype) { Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { try { Type ret = asm.GetType(vtype, false, true); if (ret != null) { return ret; } } catch (Exception err) { Console.Write(ReflectionUtil.exception2String(err)); } } return null; } public static List GetSubTypes(Type superType) { HashMap map = new HashMap(); foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { try { Type[] types = asm.GetTypes(); foreach (Type sub in types) { if (sub.IsClass && superType.IsAssignableFrom(sub)) { map.Put(sub.FullName, sub); } } } catch (Exception err) { Console.Write(ReflectionUtil.exception2String(err)); } } List ret = new List(map.Values); ret.Sort(new TypeComparer()); return ret; } /// /// 获取所有匹配的非虚子类 /// /// /// public static List GetNoneVirtualSubTypes(Type superType) { HashMap map = new HashMap(); Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { try { Type[] types = asm.GetTypes(); foreach (Type sub in types) { if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub)) { map.Put(sub.FullName, sub); } } } catch (Exception err) { Console.Write(ReflectionUtil.exception2String(err)); } } List ret = new List(map.Values); ret.Sort(new TypeComparer()); return ret; } static string exception2String(Exception e) { if (e is ReflectionTypeLoadException) { var sb = new StringBuilder(); foreach (var le in (e as ReflectionTypeLoadException).LoaderExceptions) { sb.Append(le.Message).Append("\r\n"); } return sb.ToString(); } return e.ToString(); } public static List GetNoneVirtualSubTypes(Type superType, Assembly asm) { HashMap map = new HashMap(); Type[] types = asm.GetTypes(); foreach (Type sub in types) { if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub)) { map.Put(sub.FullName, sub); } } List ret = new List(map.Values); ret.Sort(new TypeComparer()); return ret; } /// /// 获取所有匹配的非虚子类,每个类必须标注DescAttribute /// /// /// public static List GetNoneVirtualSubDescTypes(Type superType) { HashMap map = new HashMap(); Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { try { Type[] types = asm.GetTypes(); foreach (Type sub in types) { if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub)) { DescAttribute attr = PropertyUtil.GetAttribute(sub); if (attr != null) { map.Put(sub.FullName, new TypeDescAttribute(sub)); } } } } catch (Exception err) { Console.Write(ReflectionUtil.exception2String(err)); } } List ret = new List(map.Values); ret.Sort(new ToStringComparer()); return ret; } public static List GetNoneVirtualSubDescTypes(Type superType, Assembly asm) { HashMap map = new HashMap(); Type[] types = asm.GetTypes(); foreach (Type sub in types) { if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub)) { DescAttribute attr = PropertyUtil.GetAttribute(sub); if (attr != null) { map.Put(sub.FullName, new TypeDescAttribute(sub)); } } } List ret = new List(map.Values); ret.Sort(new ToStringComparer()); return ret; } } }