如何两个对象数据比对(如何进行两组数据的比对)

网友投稿 287 2022-06-24


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 in ps

where 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, List dictionary) 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, object model)

{

if (model == null)

{

model = new T();

}

var dy = (from p in property

where p.GetCustomAttribute(true) != null

select new

{

name = p.Name,

dname = p.GetCustomAttribute(true).DisplayName,

value = GetModelValue(model, p),

});

return dy;

}

///

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

///

/// 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, object model)

{

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

var dy = (from p in property

where p.GetCustomAttribute(true) != null

select new

{

name = p.Name,

dname = p.GetCustomAttribute(true).IsRequired,

value = GetModelValue(model, p),

});

return dy;

}

static IEnumerable GetModel(PropertyInfo[] property, object model)

{

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

var dy = (from p in property

where p.GetCustomAttribute(true) != null

select new

{

name = p.Name,

dname = p.GetCustomAttribute(true).IsRequired,

value = GetModelValue(model, p),

});

return dy;

}

static object GetModelValue(object obj, 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);

}

return value;

}

static bool Isfield(object value)

{

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;

}

}

static Type 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 class ModelEqualDictionary

{

public string DisplayName { get; set; }

public object LostValue { get; set; }

public object NewValue { get; set; }

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:教你配置windows上的windbg,linux上的lldb,打入clr内部这一篇就够了
下一篇:Expression拼接
相关文章

 发表评论

暂时没有评论,来抢沙发吧~