| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace HXX.Scanner.Common
- {
- public class EntityClone
- {
- /// <summary>
- /// 可父类向子类拷贝
- /// </summary>
- /// <typeparam name="Tp"></typeparam>
- /// <typeparam name="Tc"></typeparam>
- /// <param name="parent"></param>
- /// <returns></returns>
- public static Tc DeriveCopy<Tp, Tc>(Tp parent) where Tc : new()
- {
- try
- {
- Tc child = new Tc();
- var ParentType = typeof(Tp);
- var Properties = ParentType.GetProperties();
- foreach (var Propertie in Properties)
- {
- if (Propertie.CanRead && Propertie.CanWrite)
- {
- Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
- }
- }
- var fields = ParentType.GetFields();
- foreach (var f in fields)
- {
- f.SetValue(child, f.GetValue(parent));
- }
- return child;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
|