用python怎么做数据比对_如何两个对象数据比对|简明python教程|python入门|python教程...

public partial class ModelStatusDictionary where T : new()

{static readonly DynamicMethod _dynamicMethod = new DynamicMethod();///

///判断指定特性的属性///

/// 对象

/// 是否获取基类的属性

/// 排除的属性

///

public static (bool state, string message) ModelVerified(T obj, bool NonStance = true, ICollection NonProperty = null)

{bool isEmpty = true;string msge = string.Empty;var ps = typeof(T).GetProperties();if (!NonStance)

ps= typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static |BindingFlags.Instance);if (NonProperty != null)

ps= ps.Where(x => !NonProperty.Contains(x.Name)).ToArray();var vModel = (from p inpswhere p.GetCustomAttribute(true) != null

select new{

name=p.Name,

dname= p.GetCustomAttribute(true).DisplayName,

velue=_dynamicMethod.GetValue(obj, p.Name),

}).Distinct().ToList();var fiel = vModel.Where(x => !Isfield(x.velue)).ToList();if (fiel.Count > 0)

{

isEmpty= false;

msge= $"{string.Join(",", fiel.Select(x => x.dname).ToList())},Required fields cannot be empty!";

}return(isEmpty, msge);

}///

///两个类型一样的对象做比较///

/// A

/// B

public static (bool state, Listdictionary) ModelEquals(T lastModel, T newModel)

{bool isEquals = true;var ps = typeof(T).GetProperties();var _last = GetObject(ps, lastModel);//(from p in ps

var _new =GetObject(ps, newModel);var fields = _new.Except(_last).Where(x => !string.IsNullOrWhiteSpace(x.dname)).Select(x => new ModelEqualDictionary() { DisplayName = x.dname, LostValue = _last.Where(y => y.name == x.name).FirstOrDefault().value, NewValue =x.value }).ToList();if (fields != null)if(fields.Any())

isEquals= false;return(isEquals, fields);

}static IEnumerable GetObject(PropertyInfo[] property, objectmodel)

{if (model == null)

{

model= newT();

}var dy = (from p inpropertywhere p.GetCustomAttribute(true) != null

select new{

name=p.Name,

dname= p.GetCustomAttribute(true).DisplayName,

value=GetModelValue(model, p),

});returndy;

}///

///两个类型一样的对象做比较///

/// A

/// B

/// C

public static (bool state, List dictionary) ModelEqual(T lastModel, T newModel,bool nonProperty = false,ICollection NonProperty = null)

{bool isEquals = true;var ps = typeof(T).GetProperties();if (NonProperty != null)

{

ps= ps.Where(x => !NonProperty.Contains(x.Name)).ToArray();

}var _last = nonProperty ?GetNonPropertyModel(ps,lastModel): GetModel(ps, lastModel);var _new = nonProperty ?GetNonPropertyModel(ps, newModel) : GetModel(ps, newModel);var fields = _new.Except(_last).Select(x => new ModelEqualDictionary() { DisplayName = x.dname, LostValue = _last.Where(y => y.name == x.name).FirstOrDefault().value, NewValue =x.value }).ToList();if (fields != null)if(fields.Any())

isEquals= false;return(isEquals, fields);

}static IEnumerable GetNonPropertyModel(PropertyInfo[] property, objectmodel)

{var a = property.Where(p => p.GetCustomAttribute(true) != null).ToList();var dy = (from p inpropertywhere p.GetCustomAttribute(true) != null

select new{

name=p.Name,

dname= p.GetCustomAttribute(true).IsRequired,

value=GetModelValue(model, p),

});returndy;

}static IEnumerable GetModel(PropertyInfo[] property, objectmodel)

{var a = property.Where(p => p.GetCustomAttribute(true) != null).ToList();var dy = (from p inpropertywhere p.GetCustomAttribute(true) != null

select new{

name=p.Name,

dname= p.GetCustomAttribute(true).IsRequired,

value=GetModelValue(model, p),

});returndy;

}static object GetModelValue(objectobj, PropertyInfo property)

{object value = string.Empty;if (getPropertyType(property.PropertyType) == typeof(Nullable))

{var nextObj = obj.GetType().GetProperty(property.Name).GetValue(obj, null);if (property.PropertyType.BaseType.Name == "Enum")

{

value=nextObj;

}else if (nextObj != null)

{if (getPropertyType(nextObj.GetType()) == typeof(DateTime))

{

value=nextObj;

}else{var ps =nextObj.GetType().GetProperties();var thisObj =GetObject(ps, nextObj);

value= string.Join(",", thisObj ?? thisObj.Select(x =>x.value).ToList());

}

}

}else{

value= obj.GetType().GetProperty(property.Name).GetValue(obj, null);

}returnvalue;

}static bool Isfield(objectvalue)

{if (value == null) return false;var val =value.GetType().ToString();switch(val)

{case "System.Boolean":return value == null ? false : true;case "System.Byte":return value == null ? false : true;case "System.SByte":return value == null ? false : true;case "System.Char":return string.IsNullOrWhiteSpace(value.ToString()) ? false : true;case "System.Decimal":return value == null ? false : true;case "System.Double":return value == null ? false : true;case "System.Single":return value == null ? false : true;case "System.Int32":return value == null ? false : true;case "System.UInt32":return value == null ? false : true;case "System.Int64":return value == null ? false : true;case "System.UInt64":return value == null ? false : true;case "System.Object":return value == null ? false : true;case "System.Int16":return value == null ? false : true;case "System.UInt16":return value == null ? false : true;case "System.String":return string.IsNullOrWhiteSpace(value.ToString()) ? false : true;case "System.DateTime.Date":return value == null ? false : true;case "System.DateTime":return value == null ? false : true;case "System.Guid":return value == null ? false : true;default:return true;

}

}staticType getPropertyType(Type obj)

{if (obj == null) return typeof(DBNull);var val =obj.ToString();switch(val)

{case "System.Boolean":return typeof(Boolean);case "System.Byte":return typeof(Byte);case "System.SByte":return typeof(SByte);case "System.Char":return typeof(char);case "System.Decimal":return typeof(Decimal);case "System.Double":return typeof(Double);case "System.Single":return typeof(Single);case "System.Int32":return typeof(Int32);case "System.UInt32":return typeof(UInt32);case "System.Int64":return typeof(Int64);case "System.UInt64":return typeof(UInt64);case "System.Object":return typeof(Object);case "System.Int16":return typeof(Int16);case "System.UInt16":return typeof(UInt16);case "System.String":return typeof(string);case "System.DateTime.Date":return typeof(DateTime);case "System.DateTime":return typeof(DateTime);case "System.Guid":return typeof(Guid);case "System.Enum":return typeof(Enum);default:return typeof(Nullable);

}

}

}public classModelEqualDictionary

{public string DisplayName { get; set; }public object LostValue { get; set; }public object NewValue { get; set; }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值