• 自定义属性


    属性定义

        [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
        public class ColumnNameAttribute : Attribute
        {
            private string _columnName;
            public ColumnNameAttribute(string columnName, string columnChsName=null)
            {
                this._columnName = columnName;
                this._columnChsName = columnChsName;
            }
    
            public string ColumnName
            {
                get { return _columnName; }
            }
    
            private string _columnChsName;
    
            public string ColumnChsName
            {
                get { return _columnChsName; }
            }
        }
    

      属性使用

    var properties = typeof(T).GetProperties().Where(v => v.IsDefined(typeof(ColumnNameAttribute), true)).ToList();
    
     foreach (var pro in properties)
                {
                    var sourctValue = GetString(pro.GetValue(sourceObj, null));
                    var newValue = GetString(pro.GetValue(newObj, null));
                    if (sourctValue != newValue)
                    {
                        string colName = ((ColumnNameAttribute)pro.GetCustomAttributes(typeof(ColumnNameAttribute), true)[0]).ColumnChsName;
                        logContent.AppendLine("</br>" + colName + ":从" + sourctValue + " 变更到 " + newValue);
                        hasChanged = true;
                    }
                }
    

      私有方法

            private static string GetString(object o)
            {
                if (o == null || o== DBNull.Value)
                {
                    return string.Empty;
                }
                else
                {
                    return o.ToString();
                }
            }
    

      

  • 相关阅读:
    7.数组的扩展
    8.对象的扩展
    6.函数的扩展
    5.数值的扩展
    2.变量的解构赋值
    1.let 和 const 命令
    CTE(With As)
    delphi使用ADO在sql数据库存取图片的方法
    使用Razor生成Word
    Redis基础总结
  • 原文地址:https://www.cnblogs.com/xh831213/p/5085060.html
Copyright © 2020-2023  润新知