/// <summary> /// 对象属性拷贝(同名字段) /// </summary> /// <param name="source">源对象</param> /// <param name="target">目标对象</param> /// <returns></returns> public static int ObjectCopy(object source, object target) { if (source == null || target == null) { return(0); } int ret = 0; Type sourceType = source.GetType(); Type targetType = source.GetType(); ClassInfoHandle sourceInfo = ClassInfoManager.GetClassHandle(sourceType); ClassInfoHandle targetInfo = ClassInfoManager.GetClassHandle(targetType); foreach (FieldInfoHandle fInfo in sourceInfo.FieldInfo) { FieldInfoHandle tInfo = targetInfo.FieldInfo[fInfo.FieldName]; if (tInfo != null) { if (tInfo.FieldType == fInfo.FieldType) { tInfo.SetValue(target, fInfo.GetValue(source)); ret++; } } } return(ret); }
/// <summary> /// 初始化 /// </summary> /// <param name="assembly">程序集</param> /// <param name="TypeName">类型名</param> private void Init(Type tp, params object[] args) { _classHandle = ClassInfoManager.GetClassHandle(tp); _instance = Activator.CreateInstance(tp, args); }