对象工厂类,通过反射生产对象,可生成程序集里的对象,无需在项目里引用dll,只需传入dll路径和类相关信息即可。
/// <summary>
/// 对象工厂
/// by yanhang0610
/// </summary>
public class BeanFactory {
private static Dictionary<String, Type> classFullPathNames = new Dictionary<String, Type>();
/// <summary>
/// 通过反射生成对象
/// </summary>
/// <param name="classFullPathNameWithAssembly">格式:"程序集(Dll绝对路径,包含dll后缀);类名全路径",程序集和类路径之间以半角分号分隔。如果是本程序集内的类,则只含类名全路径,不含分号和程序集</param>
/// <returns></returns>
public static Object CreateBean(String classFullPathNameWithAssembly, Object[] args = null) {
if (classFullPathNameWithAssembly.IndexOf(';') < 0) {
return BeanFactory.CreateBean(Assembly.GetExecutingAssembly(), classFullPathNameWithAssembly, args);
} else {
String[] param = classFullPathNameWithAssembly.Split(';');
return BeanFactory.CreateBean(Assembly.LoadFile(param[0].Trim()), param[1].Trim(), args);
}
}
public static Object CreateBean(Assembly assembly, String classFullPathName) {
return BeanFactory.CreateBean(assembly, classFullPathName, null);
}
public static Object CreateBean(Assembly assembly, String classFullPathName, Object[] args) {
try {
String key = assembly.FullName + "_" + classFullPathName;
Type type = null;
if (!BeanFactory.classFullPathNames.ContainsKey(key)) {
type = assembly.GetType(classFullPathName);
BeanFactory.classFullPathNames.Add(key, type);
} else {
type = BeanFactory.classFullPathNames[key];
}
Object obj = Activator.CreateInstance(type, args);
return obj;
} catch (Exception e) {
throw new Exception("实例化异常", e);
}
}
public static Object ExecuteMethod(Assembly assembly, String classFullPathName, String methodName) {
return ExecuteMethod(assembly, classFullPathName, null, methodName, null);
}
public static Object ExecuteMethod(Assembly assembly, String classFullPathName, String methodName, Object[] methodParams) {
return ExecuteMethod(assembly, classFullPathName, null, methodName, methodParams);
}
public static Object ExecuteMethod(Assembly assembly, String classFullPathName, Object[] constructParams, String methodName, Object[] methodParams) {
try {
Object bean = BeanFactory.CreateBean(assembly, classFullPathName, constructParams);
MethodInfo method = bean.GetType().GetMethod(methodName);
return method.Invoke(bean, methodParams);
} catch (Exception e) {
throw new Exception("反射调用Dll里类方法[" + classFullPathName + "#" + methodName + "]异常!", e);
}
}
public static Object ExecuteMethod(Object entity, String methodName) {
return ExecuteMethod(entity, methodName, null);
}
public static Object ExecuteMethod(Object entity, String methodName, Object[] methodParams) {
try {
MethodInfo method = entity.GetType().GetMethod(methodName);
return method.Invoke(entity, methodParams);
} catch (Exception e) {
throw new Exception("反射调对象方法[" + methodName + "]异常!", e);
}
}
/// <summary>
/// 获取类对象的指定名称的公共字段值
/// </summary>
/// <param name="classFullPathNameWithAssembly">格式:"程序集(Dll绝对路径,包含dll后缀);类名全路径",程序集和类路径之间以半角分号分隔。如果是本程序集内的类,则只含类名全路径,不含分号和程序集</param>
/// <param name="fieldName">公共字段名称</param>
/// <param name="args">构造函数参数,可选</param>
/// <returns></returns>
public static Object GetBeanFieldValue(String classFullPathNameWithAssembly, String fieldName, Object[] args = null) {
try {
Object obj = BeanFactory.CreateBean(classFullPathNameWithAssembly, args);
Type type = obj.GetType();
FieldInfo[] fields = type.GetFields();
FieldInfo field = fields.FirstOrDefault(p => fieldName.Equals(p.Name));
return field.GetValue(obj);
} catch (Exception) {
return null;
}
}
/// <summary>
/// 深层复制
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="srcObj"></param>
/// <returns></returns>
public static T Clone<T>(T srcObj) {
using (Stream objectStream = new MemoryStream()) {
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, srcObj);
objectStream.Seek(0, SeekOrigin.Begin);
return (T) formatter.Deserialize(objectStream);
}
}
}