1. 程式人生 > >將一個類例項中所有屬性值賦給另一個同類型的物件【泛型+Linq】

將一個類例項中所有屬性值賦給另一個同類型的物件【泛型+Linq】

public static class ClassCopy
    {
        static ConcurrentDictionary<string, object> actions = new ConcurrentDictionary<string, object>();

        static Action<S, T> CreateCopier<S, T>()
        {
            var target = Expression.Parameter(typeof(T));
            var source = Expression.Parameter(typeof(S));
            var props1 = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).ToList();
            var props2 = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite).ToList();
            var props = props1.Where(x => props2.Where(y => y.Name == x.Name).Count() > 0);
            var block = Expression.Block(from p in props select Expression.Assign(Expression.Property(target, p.Name), Expression.Property(source, p.Name)));
            return Expression.Lambda<Action<S, T>>(block, source, target).Compile();
        }

        public static T ShadowCopy<T>(this T original) where T : class, new()
        {
            T target = new T();
            string name = string.Format("{0}", typeof(T));
            object obj;
            if (!actions.TryGetValue(name, out obj))
            {
                var ff = CreateCopier<T, T>();
                actions.TryAdd(name, ff);
                obj = ff;
            }
            Action<T, T> act = (Action<T, T>)obj;
            act(original, target);
            return target;
        }
    }

用法:

wiplotsts = wiplotsts2.ShadowCopy();

==========原始的做法

wiplotsts = new Wiplotsts()
                {

                    LotId = wiplotsts2.LotId,
                    LotDesc = wiplotsts2.LotDesc,
                    Factory = wiplotsts2.Factory,
                    MatId = wiplotsts2.MatId,
                    MatVer = wiplotsts2.MatVer,
                    Flow = wiplotsts2.Flow,
                    FlowSeqNum = wiplotsts2.FlowSeqNum,
                    Oper = wiplotsts2.Oper,
                    Qty1 = wiplotsts2.Qty1,
                    Qty2 = wiplotsts2.Qty2,
                    Qty3 = wiplotsts2.Qty3,
                    CrrId = wiplotsts2.CrrId,
                    LotType = wiplotsts2.LotType,
                    OwnerCode = wiplotsts2.OwnerCode,
                    CreateCode = wiplotsts2.CreateCode,
                    LotPriority = wiplotsts2.LotPriority,
                    LotStatus = wiplotsts2.LotStatus,
                    HoldFlag = wiplotsts2.HoldFlag,
                    HoldCode = wiplotsts2.HoldCode,
                    HoldPassword = wiplotsts2.HoldPassword,
                    HoldPrvGrpId = wiplotsts2.HoldPrvGrpId,
                    OperInQty1 = wiplotsts2.OperInQty1,
                    OperInQty2 = wiplotsts2.OperInQty2,
                    OperInQty3 = wiplotsts2.OperInQty3,
                    CreateQty1 = wiplotsts2.CreateQty1,
                    CreateQty2 = wiplotsts2.CreateQty2,
                    CreateQty3 = wiplotsts2.CreateQty3,
                    StartQty1 = wiplotsts2.StartQty1,
                    StartQty2 = wiplotsts2.StartQty2,
                    StartQty3 = wiplotsts2.StartQty3,
                    InvFlag = wiplotsts2.InvFlag,
                    TransitFlag = wiplotsts2.TransitFlag,
                    UnitExistFlag = wiplotsts2.UnitExistFlag,
                    InvUnit = wiplotsts2.InvFlag,
                    RwkFlag = wiplotsts2.RwkFlag,
                    RwkCode = wiplotsts2.RwkCode,
                    RwkCount = wiplotsts2.RwkCount,
                    RwkRetFlow = wiplotsts2.RwkRetFlow,
                    RwkRetFlowSeqNum = wiplotsts2.RwkRetFlowSeqNum,
                    RwkRetOper = wiplotsts2.RwkRetOper,
                    RwkEndFlow = wiplotsts2.RwkEndFlow,
                    RwkEndFlowSeqNum = wiplotsts2.RwkEndFlowSeqNum,
                    RwkEndOper = wiplotsts2.RwkEndOper,
                    RwkRetClearFlag = wiplotsts2.RwkRetClearFlag,
                    RwkTime = wiplotsts2.RwkTime,
                    NstdFlag = wiplotsts2.NstdFlag,
                    NstdRetFlow = wiplotsts2.NstdRetFlow,
                    NstdRetFlowSeqNum = wiplotsts2.NstdRetFlowSeqNum,
                    NstdRetOper = wiplotsts2.NstdRetOper,
                    NstdTime = wiplotsts2.NstdTime,
                    RepFlag = wiplotsts2.RepFlag,
                    RepRetOper = wiplotsts2.RepRetOper,
                    StrRetFlow = wiplotsts2.StrRetFlow,
                    StrRetFlowSeqNum = wiplotsts2.StrRetFlowSeqNum,
                    StrRetOper = wiplotsts2.StrRetOper,
                    StartFlag = wiplotsts2.StartFlag,
                    StartTime = wiplotsts2.StartTime,
                    StartResId = wiplotsts2.StartResId,
                    EndFlag = wiplotsts2.EndFlag,
                    EndTime = wiplotsts2.EndTime,
                    EndResId = wiplotsts2.EndResId,
                    SampleFlag = wiplotsts2.SampleFlag,
                    SampleWaitFlag = wiplotsts2.SampleWaitFlag,
                    SampleResult = wiplotsts2.SampleResult,
                    FromToFlag = wiplotsts2.FromToFlag,
                    FromToLotId = wiplotsts2.FromToLotId,
                    ShipCode = wiplotsts2.ShipCode,
                    ShipTime = wiplotsts2.ShipTime,
                    OrgDueTime = wiplotsts2.OrgDueTime,
                    SchDueTime = wiplotsts2.SchDueTime,
                    CreateTime = wiplotsts2.CreateTime,
                    FacInTime = wiplotsts2.FacInTime,
                    FlowInTime = wiplotsts2.FlowInTime,
                    OperInTime = wiplotsts2.OperInTime,
                    ReserveResId = wiplotsts2.ReserveResId,
                    PortId = wiplotsts2.PortId,
                    BatchId = wiplotsts2.BatchId,
                    BatchSeq = wiplotsts2.BatchSeq,
                    OrderId = wiplotsts2.OrderId,
                    AddOrderId1 = wiplotsts2.AddOrderId1,
                    AddOrderId2 = wiplotsts2.AddOrderId2,
                    AddOrderId3 = wiplotsts2.AddOrderId3,
                    LotLocation1 = wiplotsts2.LotLocation1,
                    LotLocation2 = wiplotsts2.LotLocation2,
                    LotLocation3 = wiplotsts2.LotLocation3,
                    LotCmf1 = wiplotsts2.LotCmf1,
                    LotCmf2 = wiplotsts2.LotCmf2,
                    LotCmf3 = wiplotsts2.LotCmf3,
                    LotCmf4 = wiplotsts2.LotCmf4,
                    LotCmf5 = wiplotsts2.LotCmf5,
                    LotCmf6 = wiplotsts2.LotCmf6,
                    LotCmf7 = wiplotsts2.LotCmf7,
                    LotCmf8 = wiplotsts2.LotCmf8,
                    LotCmf9 = wiplotsts2.LotCmf9,
                    LotCmf10 = wiplotsts2.LotCmf10,
                    LotCmf11 = wiplotsts2.LotCmf11,
                    LotCmf12 = wiplotsts2.LotCmf12,
                    LotCmf13 = wiplotsts2.LotCmf13,
                    LotCmf14 = wiplotsts2.LotCmf14,
                    LotCmf15 = wiplotsts2.LotCmf15,
                    LotCmf16 = wiplotsts2.LotCmf16,
                    LotCmf17 = wiplotsts2.LotCmf17,
                    LotCmf18 = wiplotsts2.LotCmf18,
                    LotCmf19 = wiplotsts2.LotCmf19,
                    LotCmf20 = wiplotsts2.LotCmf20,
                    LotDelFlag = wiplotsts2.LotDelFlag,
                    LotDelCode = wiplotsts2.LotDelCode,
                    LotDelTime = wiplotsts2.LotDelTime,
                    BomSetId = wiplotsts2.BomSetId,
                    BomSetVersion = wiplotsts2.BomSetVersion,
                    BomActiveHistSeq = wiplotsts2.BomActiveHistSeq,
                    BomHistSeq = wiplotsts2.BomHistSeq,
                    LastTranCode = wiplotsts2.LastTranCode,
                    LastTranTime = wiplotsts2.LastTranTime,
                    LastComment = wiplotsts2.LastComment,
                    LastActiveHistSeq = wiplotsts2.LastActiveHistSeq,
                    LastHistSeq = wiplotsts2.LastHistSeq,
                    CriticalResId = wiplotsts2.CriticalResId,
                    CriticalResGroupId = wiplotsts2.CriticalResGroupId,
                    SaveResId1 = wiplotsts2.SaveResId1,
                    SaveResId2 = wiplotsts2.SaveResId2,
                    SubresId = wiplotsts2.SubresId,
                    LotGroupId1 = wiplotsts2.LotGroupId1,
                    LotGroupId2 = wiplotsts2.LotGroupId2,
                    LotGroupId3 = wiplotsts2.LotGroupId3,
                    Yield1 = wiplotsts2.Yield1,
                    Yield2 = wiplotsts2.Yield2,
                    Yield3 = wiplotsts2.Yield3,
                    GoodQty = wiplotsts2.GoodQty,
                    ResvField1 = wiplotsts2.ResvField1,
                    ResvField2 = wiplotsts2.ResvField2,
                    ResvField3 = wiplotsts2.ResvField3,
                    ResvField4 = wiplotsts2.ResvField4,
                    ResvField5 = wiplotsts2.ResvField5,
                    ResvFlag1 = wiplotsts2.ResvFlag1,
                    ResvFlag2 = wiplotsts2.ResvFlag2,
                    ResvFlag3 = wiplotsts2.ResvFlag3,
                    ResvFlag4 = wiplotsts2.ResvFlag4,
                    ResvFlag5 = wiplotsts2.ResvFlag5,
                };