属性定义

    [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();
            }
        }

  

相关文章:

  • 2021-10-21
  • 2021-09-18
  • 2021-04-20
  • 2021-12-24
  • 2021-08-14
  • 2021-07-05
  • 2021-06-27
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案