/// <summary> /// 克隆对象 /// </summary> /// <returns></returns> internal Object Clone() { return(PersistentObjectFactory.CloneEopBaseObject(this)); }
/// <summary> /// 以传入对象为母版克隆对象 /// </summary> /// <param name="sourceObj"></param> /// <returns></returns> public static object CloneEopBaseObject(object sourceObj) { if (sourceObj == null) { return(null); } Type objType = sourceObj.GetType(); if (objType.IsValueType) { return(null); } object newObj = Activator.CreateInstance(objType); // 复制属性值 // 取出所有Public属性,如果有Set方法,则赋值 // 如果是Class则递归调用,如果是结构体,则要用默认构造创建 // 获取类所有Public属性 PropertyInfo[] properties = GetProperties(objType); // 如果有BeginInit方法则调用 MethodInfo beginInitMethod = GetMethod(objType, "BeginInit"); if (beginInitMethod != null) { beginInitMethod.Invoke(newObj, null); } object objValue; foreach (PropertyInfo prop in properties) { if (!prop.CanWrite) { continue; } objValue = prop.GetValue(sourceObj, null); if ((prop.PropertyType.IsValueType) || (prop.PropertyType == typeof(string))) { prop.SetValue(newObj, objValue, null); } else if (prop.PropertyType.IsClass) { if (prop.PropertyType.BaseType == typeof(MulticastDelegate)) { prop.SetValue(newObj, objValue, null); } else { object obj = PersistentObjectFactory.CloneEopBaseObject(objValue); prop.SetValue(newObj, obj, null); } } } // 如果有EndInit方法则调用 MethodInfo endInitMethod = GetMethod(objType, "EndInit"); if (endInitMethod != null) { endInitMethod.Invoke(newObj, null); } return(newObj); }