123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- using System.IO;
- using CommonLang.Log;
- namespace CommonLang.Property
- {
- public class PropertyList<T> : List<T> where T : new()
- {
- public PropertyList()
- {
- }
- public PropertyList(T[] other)
- {
- AddRange(other);
- }
- public PropertyList(ICollection<T> other)
- {
- AddRange(other);
- }
- }
- public class TypeComparer : IComparer<Type>
- {
- public int Compare(Type x, Type y)
- {
- return x.FullName.CompareTo(y.FullName);
- }
- }
- public class ToStringComparer<T> : IComparer<T>
- {
- 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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(string type)
- {
- return CreateInterface<T>(type, new object[0]);
- }
- public static T CreateInterface<T>(Type data_plugin_type)
- {
- return CreateInterface<T>(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<Type> GetSubTypes(Type superType)
- {
- HashMap<string, Type> map = new HashMap<string, Type>();
- 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<Type> ret = new List<Type>(map.Values);
- ret.Sort(new TypeComparer());
- return ret;
- }
- /// <summary>
- /// 获取所有匹配的非虚子类
- /// </summary>
- /// <param name="superType"></param>
- /// <returns></returns>
- public static List<Type> GetNoneVirtualSubTypes(Type superType)
- {
- HashMap<string, Type> map = new HashMap<string, Type>();
- 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<Type> ret = new List<Type>(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<Type> GetNoneVirtualSubTypes(Type superType, Assembly asm)
- {
- HashMap<string, Type> map = new HashMap<string, Type>();
- Type[] types = asm.GetTypes();
- foreach (Type sub in types)
- {
- if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
- {
- map.Put(sub.FullName, sub);
- }
- }
- List<Type> ret = new List<Type>(map.Values);
- ret.Sort(new TypeComparer());
- return ret;
- }
- /// <summary>
- /// 获取所有匹配的非虚子类,每个类必须标注DescAttribute
- /// </summary>
- /// <param name="superType"></param>
- /// <returns></returns>
- public static List<TypeDescAttribute> GetNoneVirtualSubDescTypes(Type superType)
- {
- HashMap<string, TypeDescAttribute> map = new HashMap<string, TypeDescAttribute>();
- 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<DescAttribute>(sub);
- if (attr != null)
- {
- map.Put(sub.FullName, new TypeDescAttribute(sub));
- }
- }
- }
- }
- catch (Exception err)
- {
- Console.Write(ReflectionUtil.exception2String(err));
- }
- }
- List<TypeDescAttribute> ret = new List<TypeDescAttribute>(map.Values);
- ret.Sort(new ToStringComparer<TypeDescAttribute>());
- return ret;
- }
- public static List<TypeDescAttribute> GetNoneVirtualSubDescTypes(Type superType, Assembly asm)
- {
- HashMap<string, TypeDescAttribute> map = new HashMap<string, TypeDescAttribute>();
- Type[] types = asm.GetTypes();
- foreach (Type sub in types)
- {
- if (sub.IsClass && !sub.IsAbstract && superType.IsAssignableFrom(sub))
- {
- DescAttribute attr = PropertyUtil.GetAttribute<DescAttribute>(sub);
- if (attr != null)
- {
- map.Put(sub.FullName, new TypeDescAttribute(sub));
- }
- }
- }
- List<TypeDescAttribute> ret = new List<TypeDescAttribute>(map.Values);
- ret.Sort(new ToStringComparer<TypeDescAttribute>());
- return ret;
- }
- }
- }
|