码迷,mamicode.com
首页 > Windows程序 > 详细

C#实体类对象修改日志记录

时间:2018-12-28 18:28:16      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:returns   this   记录   ryu   info   object   reac   bre   实体类   

C#实体类对象修改日志记录

类型验证帮助类


    public static class TypeExtensions
    {
        public static bool InheritsFrom(this Type source, Type target)
        {
            if (null == source || null == target)
            {
                return false;
            }

            if (source == target)
            {
                return true;
            }

            if (source.GetTypeInfo().IsGenericType && source.GetTypeInfo().GetGenericTypeDefinition() == target)
            {
                return true;
            }

            if (source.GetTypeInfo().GetInterfaces().Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == target || i == target))
            {
                return true;
            }

            return source.GetTypeInfo().BaseType != null &&
                   InheritsFrom(source.GetTypeInfo().BaseType, target);
        }

    }

实体变更记录类

public class ModelHistoryUtils
{
    
     /// <summary>
        /// 获取实体变更记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetRecord(object model)
        {
            if (null == model)
            {
                return string.Empty;
            }
            Type type = model.GetType();
            StringBuilder sbResult = new StringBuilder("<ul>");
            try
            {
                if (typeof(string) == type || type.IsPrimitive)
                {
                    sbResult.Append(model.ToString());
                }
                else if (type.InheritsFrom(typeof(Dictionary<,>)))
                {
                    StringBuilder sbDic = new StringBuilder("<ul>");
                    IDictionary dictionary = ((IDictionary)model);
                    foreach (var key in dictionary.Keys)
                    {
                        sbDic.Append($"<li><b>{GetRecord(key, false)}:</b>{GetRecord(dictionary[key], false)}</li>");
                    }
                    sbResult.Append("</ul>");
                }
                else if (type.InheritsFrom(typeof(IEnumerable)))
                {
                    List<string> lstRecord = ((IEnumerable)model)
                        .Cast<object>()
                        .Select(item => $"<li>{GetRecord(item, false)}</li>")
                        .ToList() ?? new List<string>();
                    sbResult.Append($"<ul>{string.Join("", lstRecord)}</ul>");
                }
                else
                {
                    PropertyInfo[] lstProperty = type.GetProperties();
                    DescriptionAttribute description;
                    foreach (PropertyInfo prop in lstProperty)
                    {
                        description = prop.GetCustomAttribute<DescriptionAttribute>();
                        if (null != description)
                        {
                            string value = "";
                            if (prop.PropertyType.InheritsFrom(typeof(IEnumerable)) && typeof(string) != prop.PropertyType)
                            {
                                value = GetRecord(prop.GetValue(model), false);
                            }
                            else
                            {
                                value = prop.GetValue(model)?.ToString() ?? "";
                            }
                            sbResult.Append($"<li><b>{description.Description}:</b>{value}</li>");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            sbResult.Append("</ul>");
            return sbResult.ToString();
        }
}

C#实体类对象修改日志记录

标签:returns   this   记录   ryu   info   object   reac   bre   实体类   

原文地址:https://www.cnblogs.com/AlvinLee/p/10192011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!