You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
2.5 KiB

using System;
using System.Collections;
using System.Reflection;
namespace Znyc.Cloudcar.Admin.Commons.Extend
{
/// <summary>
/// 根据业务对象的类型进行反射操作辅助类
/// </summary>
/// <typeparam name="T"></typeparam>
public class Reflect<T> where T : class
{
private static readonly Hashtable ObjCache = new();
private static readonly object syncRoot = new();
/// <summary>
/// 根据参数创建对象实例
/// </summary>
/// <param name="sName">对象全局名称</param>
/// <param name="sFilePath">文件路径</param>
/// <returns></returns>
public static T Create(string sName, string sFilePath)
{
return Create(sName, sFilePath, true);
}
/// <summary>
/// 根据参数创建对象实例
/// </summary>
/// <param name="sName">对象全局名称</param>
/// <param name="sFilePath">文件路径</param>
/// <param name="bCache">缓存集合</param>
/// <returns></returns>
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;
}
/// <summary>
/// 根据全名和路径构造对象
/// </summary>
/// <param name="sName">对象全名</param>
/// <param name="sFilePath">程序集路径</param>
/// <returns></returns>
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;
}
}
}