using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ProjectBase.Util { /// /// 根据业务对象的类型进行反射操作辅助类 /// /// public class Reflect where T : class { private static Hashtable ObjCache = new Hashtable(); private static object syncRoot = new Object(); /// /// 根据参数创建对象实例 /// /// 对象全局名称 /// 文件路径 /// public static T Create(string sName, string sFilePath) { return Create(sName, sFilePath, true); } /// /// 根据参数创建对象实例 /// /// 对象全局名称 /// 文件路径 /// 缓存集合 /// public static T Create(string sName, string sFilePath, bool bCache) { string CacheKey = sName; T objType = null; if (bCache) { objType = (T)ObjCache[CacheKey]; //从缓存读取 if (!ObjCache.ContainsKey(CacheKey)) { lock (syncRoot) { objType = CreateInstance(CacheKey, sFilePath); ObjCache.Add(CacheKey, objType);//缓存数据访问对象 } } } else { objType = CreateInstance(CacheKey, sFilePath); } return objType; } /// /// 根据全名和路径构造对象 /// /// 对象全名 /// 程序集路径 /// private static T CreateInstance(string sName, string sFilePath) { Assembly assemblyObj = Assembly.Load(sFilePath); if (assemblyObj == null) { throw new ArgumentNullException("sFilePath", string.Format("无法加载sFilePath={0} 的程序集", sFilePath)); } T obj = (T)assemblyObj.CreateInstance(sName); //反射创建 return obj; } } }