Example #1
0
        /// <summary>获取被更改属性列表</summary>
        /// <param name="entity">要跟踪的实体</param>
        /// <param name="refer">用于参照的实体</param>
        /// <param name="param">参数</param>
        /// <returns>被更改的集合</returns>
        public static List <Change> GetChanges(T entity, T refer, Parameters param = null)
        {
            var als = FastReflection.FastGetAccessors <T>();
            var ls  = new List <Change>(als.Count);

            foreach (var a in als.Values)
            {
                if (param.IgnoreVirtual && a.IsVirtual)
                {
                    continue;
                }
                if (!a.CanRade)
                {
                    continue;
                }

                var v1 = a.GetValue(entity);
                var v2 = a.GetValue(refer);
                if (!AreEqual(v1, v2))
                {
                    ls.Add(new Change {
                        Name = a.Name, OldValue = v1, NewValue = v2
                    });
                }
            }

            return(ls);
        }
Example #2
0
        /// <summary>克隆对象</summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">要克隆的对象</param>
        /// <returns>新对象</returns>
        public static T Clone_ <T>(this T obj)
        {
            if (obj == null)
            {
                return(default(T));
            }

            var type = obj.GetType();

            if (type.IsValueType)
            {
                return(obj);
            }

            var id = obj as IDictionary;

            if (id != null)
            {
                return((T)CloneDict(id));
            }

            var il = obj as IList;

            if (il != null)
            {
                return((T)CloneList(il));
            }

            var ic = obj as ICloneable;

            if (ic != null)
            {
                return((T)ic.Clone());
            }

            var newObj = FastReflection.FastInvoke <T>();
            var ls     = FastReflection.FastGetAccessors(type);

            foreach (var a in ls.Values)
            {
                if (!a.CanRade || !a.CanWrite)
                {
                    continue;
                }

                a.SetValue(newObj, Clone_(a.GetValue(obj)));
            }
            return(newObj);
        }