对象快照,找到对象在赋值之后有差异字段

本文介绍了如何通过对象快照的方式,来检测对象在赋值操作后发生变化的字段。通过代码示例展示了具体的实现过程,帮助开发者在代码调试和测试中定位问题。
摘要由CSDN通过智能技术生成

代码1

using System.Data;

namespace DataSync
{
   
	public partial class DynamicParameters
	{
   
		private sealed class ParamInfo
		{
   
			public string Name {
    get; set; }
			public object Value {
    get; set; }
			public DbType? DbType {
    get; set; }
		}
	}
}

代码2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace DataSync
{
   
    /// <summary>
    /// Snapshots an object for comparison later.
    /// </summary>
    public static class Snapshotter
    {
   
        /// <summary>
        /// Starts the snapshot of an object by making a copy of its current state.
        /// </summary>
        /// <typeparam name="T">The type of object to snapshot.</typeparam>
        /// <param name="obj">The object to snapshot.</param>
        /// <returns>The snapshot of the object.</returns>
        public static Snapshot<T> Start<T>(T obj)
        {
   
            return new Snapshot<T>(obj);
        }

        /// <summary>
        /// A snapshot of an object's state.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class Snapshot<T>
        {
   
            private static Func<T, T> cloner;
            private static Func<T, T, List<Change>> differ;
            private readonly T memberWiseClone;
            private readonly T trackedObject;

            /// <summary>
            /// Creates a snapshot from an object.
            /// </summary>
            /// <param name="original">The original object to snapshot.</param>
            public Snapshot(T original)
            {
   
                memberWiseClone = Clone(original);
                trackedObject = original;
            }

            /// <summary>
            /// A holder for listing new values of changes fields and properties.
            /// </summary>
            public class Change
            {
   
                /// <summary>
                /// The name of the field or property that changed.
                /// </summary>
                public string Name {
    get; set; }
                /// <summary>
                /// The new value of the field or property.
                /// </summary>
                public object NewValue {
    get; set; }
            }

            /// <summary>
            /// Does a diff between the original object and the current state.
            /// </summary>
            /// <returns>The list of the fields changes in the object.</returns>
            public DynamicParameters Diff()
            {
   
                return Diff(memberWiseClone, trackedObject);
            }

            private static T Clone(T myObject)
            {
   
                cloner = cloner ?? GenerateCloner();
                return cloner(myObject);
            }

            private static DynamicParameters Diff(T original, T current)
            {
   
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值