扫描程序集找到实现了某个接口的第一个实例
来源:互联网 发布:淘宝买家评价修改 编辑:程序博客网 时间:2024/05/22 11:55
<pre name="code" class="csharp">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Reflection;using System.Web;namespace HS.Comm.Utility{ /// <summary> /// 程序集反射辅助类 /// </summary> public static class AssemblyHelper { /// <summary> /// 得到入口程序集,兼容Web和Winform /// </summary> /// <returns></returns> public static Assembly GetEntryAssembly() { var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) return entryAssembly; if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null) return Assembly.GetExecutingAssembly(); var type = System.Web.HttpContext.Current.ApplicationInstance.GetType(); while (type != null && type.Namespace == "ASP") { type = type.BaseType; } return type == null ? null : type.Assembly; } public static IList<Stream> GetResourceStream(Assembly assembly, System.Linq.Expressions.Expression<Func<string, bool>> predicate) { List<Stream> result = new List<Stream>(); foreach (string resource in assembly.GetManifestResourceNames()) { if (predicate.Compile().Invoke(resource)) { result.Add(assembly.GetManifestResourceStream(resource)); } } StreamReader sr = new StreamReader(result[0]); string r = sr.ReadToEnd(); result[0].Position = 0; return result; } /// <summary> /// 扫描程序集找到继承了某基类的所有子类 /// </summary> /// <param name="inheritType">基类</param> /// <param name="searchpattern">文件名过滤</param> /// <returns></returns> public static List<Type> FindTypeByInheritType(Type inheritType, string searchpattern = "*.dll") { var result = new List<Type>(); Type attr = inheritType; string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { if (type.BaseType == inheritType) { result.Add(type); } } } return result; } /// <summary> /// 扫描程序集找到带有某个Attribute的所有PropertyInfo /// </summary> /// <typeparam name="T"></typeparam> /// <param name="searchpattern">文件名过滤</param> /// <returns></returns> public static Dictionary<PropertyInfo, T> FindAllPropertyByAttribute<T>(string searchpattern = "*.dll") where T : Attribute { var result = new Dictionary<PropertyInfo, T>(); var attr = typeof(T); string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { foreach (var property in type.GetProperties()) { var attrs = property.GetCustomAttributes(attr, true); if (attrs.Length == 0) continue; result.Add(property, (T)attrs.First()); } } } return result; } /// <summary> /// 扫描程序集找到类型上带有某个Attribute的所有类型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="searchpattern">文件名过滤</param> /// <returns></returns> public static Dictionary<string, List<T>> FindAllTypeByAttribute<T>(string searchpattern = "*.dll") where T : Attribute { var result = new Dictionary<string, List<T>>(); Type attr = typeof(T); string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { var typeName = type.AssemblyQualifiedName; var attrs = type.GetCustomAttributes(attr, true); if (attrs.Length == 0) continue; result.Add(typeName, new List<T>()); foreach (T a in attrs) result[typeName].Add(a); } } return result; } /// <summary> /// 扫描程序集找到实现了某个接口的第一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="searchpattern">文件名过滤</param> /// <returns></returns> public static T FindTypeByInterface<T>(string searchpattern = "*.dll") where T : class { var interfaceType = typeof(T); string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { if (interfaceType != type && interfaceType.IsAssignableFrom(type)) { var instance = Activator.CreateInstance(type) as T; return instance; } } } return null; } public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly) { if (assembly == null) throw new ArgumentNullException("assembly"); try { return assembly.GetTypes(); } catch (ReflectionTypeLoadException e) { return e.Types.Where(t => t != null); } } /// <summary> /// 得到当前应用程序的根目录 /// </summary> /// <returns></returns> public static string GetBaseDirectory() { var baseDirectory = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; if (AppDomain.CurrentDomain.SetupInformation.PrivateBinPath == null) baseDirectory = AppDomain.CurrentDomain.BaseDirectory; return baseDirectory; } }}
0 0
- 扫描程序集找到实现了某个接口的第一个实例
- 第一个graham扫描法求的凸包程序
- 判断某个类是否实现了某个接口或者继承了某个父类的方法
- 第一个Flex 的程序出炉了
- 实现Servlet接口创建第一个Servlet小程序
- 运行第一个实例display实现hello程序
- hadoop的第一个程序wordcount实现
- 第一个python实现的mapreduce程序
- 太牛逼了,找到个.NET(C#)实现的 飞鸽传书 库!
- 找到某样式下的某个结点中的第一个节点的内容(给ul li中的span赋值)
- 类型是否实现了某个接口
- 获取实现接口的的第一个泛型实例工具类
- 创建某个类型的第一个实例时所进行的操作顺序
- 第一个接口性能测试实例
- 第一个所谓的c++接口与实现的分离
- AppScan扫描出的第一个漏洞
- 今天下午完成了我的第一个JSP程序作品
- 我的第一个servlet程序终于可以运行了
- MITK交互概念
- jsp-Listener
- linux oracle 启动 关闭 服务 及监听
- ubuntu下怎么使用u盘?
- 打印x1的因子
- 扫描程序集找到实现了某个接口的第一个实例
- PostgreSQL integer out of range一例
- C++ I/O库之文件输出:ofstream
- Redis使用(1)
- ConurrentHashMap和Hashtable的区别
- Java学习笔记之Super关键字学习。
- Java 虚拟机,Reference的认识
- i386和X86各是什么意思
- oracle (+)