gpt4 book ai didi

c# - 反射(reflection):如何获得不同的IEnumerable属性?

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:13 27 4
gpt4 key购买 nike

我有一个方法可以获取相同类型的两个对象之间的差异。差异的方法存储在一个新的动态对象中。如何找到 IEnumerable 的属性之间的差异?

方法代码:

public class TestDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public Guid PassportId { get; set; }
public bool IsDeleted { get; set; }
public bool IsNotFinded { get; set; }
public bool IsInvited { get; set; }
public bool IsAuctionIsCancelled { get; set; }
public bool IsInvitationRequired { get; set; }
public string Role { get; set; }
public bool HasRetailerAccess { get; set; }
public bool HasExtendedPaidAccess { get; set; }
public bool HasRatesInPaidMode { get; set; }
public string PersonalAdminName { get; set; }
public int? PersonalAdminId { get; set; }
public bool IsPremoderationAccepted { get; set; }
public List<int> l { get; set; }

public TestDto()
{
Id = 0;
Name = "Test Name";
Login = "Test Login";
Password = "12345";
Email = "test@mail.ru";
PassportId = new Guid();
IsDeleted = false;
IsNotFinded = false;
IsInvited = false;
IsAuctionIsCancelled = false;
IsInvitationRequired = false;
HasRetailerAccess = false;
HasExtendedPaidAccess = false;
HasExtendedPaidAccess = false;
HasRatesInPaidMode = false;
IsPremoderationAccepted = false;
l = new List <int> {5, 10, 15, 100};
}
}


class Program
{
static object DifferenceOfObjects(object objectNew, object objectOld)
{
dynamic expando = new ExpandoObject();
var expandoDictionary = expando as IDictionary<String, object>;

if (objectNew.GetType() != objectOld.GetType())
{
return expando;
}
var oType = objectOld.GetType();

foreach (var oProperty in oType.GetProperties())
{
var oOldValue = oProperty.GetValue(objectOld, null);
var oNewValue = oProperty.GetValue(objectNew, null);
if (oNewValue is IEnumerable && !(oNewValue is string))
{
//Type IEnumerable property
Type itemType = oNewValue.GetType().GetGenericArguments()[0];
//How equals two IEnumerable property oOldValue and oNewValue...?
}
else
if (!Equals(oOldValue, oNewValue))
{
expandoDictionary[oProperty.Name] = new { OldValue = oOldValue, NewValue = oNewValue };
}
}

return expando;
}

static void Main(string[] args)
{
var oOldRecord = new TestDto();
var oNewRecord = new TestDto();

oNewRecord.Name = "Modifined";
oNewRecord.PersonalAdminId = 100;
oNewRecord.PassportId = Guid.NewGuid();

var diffObj = DifferenceOfObjects(oNewRecord, oOldRecord);
Console.ReadLine();
}

TestDto - 用于比较的测试类。DifferenceOfObjects - 它是发现对象之间差异的方法。

最佳答案

最简单的方法是:

bool areEqual = !list1.Except(list2).Any() && !list2.Except(list1).Any();

list1.Except(list2)检索 list1 中不在 list2 中的所有元素。 Any()检查结果列表中是否有元素。

通过这种方式,您可以检查是否没有元素不在另一个列表中。请注意,您的通用参数必须覆盖 Equals()GetHashCode() .

为了获得 IEnumerable 的通用版本,您可以使用 Cast<object>()方法。

要获得不同,您可以执行以下操作:

            var listAsObj1 = (list1 as IEnumerable).Cast<object>();
var listAsObj2 = (list2 as IEnumerable).Cast<object>();

var differences = listAsObj1.Except(listAsObj2) // In 1 (not in 2)
.Concat(listAsObj2.Except(listAsObj1)) // In 2 (not in 1)
.Select(_ => _.ToString()); // As string values

关于c# - 反射(reflection):如何获得不同的IEnumerable属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48781709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com