C#对象工厂类
来源:互联网 发布:淘宝网申请退货 编辑:程序博客网 时间:2024/06/17 17:19
对象工厂类,通过反射生产对象,可生成程序集里的对象,无需在项目里引用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);}}}
阅读全文
0 0
- C#对象工厂类
- php对象工厂类
- ObjectFactory对象工厂类
- C#面向对象的设计模式 抽象工厂模式
- c#面向对象设计模式之工厂模式
- C# 工厂
- 面向对象之多态及类工厂
- 使用DAO工厂创建实现类对象
- C#工厂模式-工厂方法
- C#工厂模式-抽象工厂
- python:类是对象,通用对象的工厂
- 2.工厂BeanFactory类,读取工厂配置文件的数据,创建Dao对象,Manager对象
- C#类和对象
- C#对象缓存类
- C#类与对象
- C# 面向对象 类
- C# 类以及对象
- C# 对象和类
- JAVA三大集合
- ipython notebook——超级强大的工具
- hdu1059 Dividing
- 质因数分解
- 时间慢些,我还没长大!
- C#对象工厂类
- Hive II
- hdu 5920 Ugly Problem 字符串处理
- 配置jupyter notebook-----更改工作目录
- 193. Valid Phone Numbers。
- bonding(绑定与删除)
- CSS3的REM设置字体大小
- [leetCode 解题报告]017. Letter Combinations of a Phone Number
- python装饰器与面向对象