C# 比较来自同一类的对象

C# 比较来自同一类的对象,c#,asp.net,.net,linq,C#,Asp.net,.net,Linq,我有下面的类,我有两个对象,一个是Obj1,一个是以前的数据,另一个是obj2,数据中有一些可以更改的字段(我正在编辑,在更改前有Obj1,在更改后有obj2)。我的问题是,如果我有两个对象,如何最好地放入对象(如键val列表)只是更改的字段,它们具有值。 我在SO上读到过这篇文章,我发现了这两种方法,但有些帖子是旧的。。。, 什么是正确/有效的方法?示例将非常有用 我在这篇文章中尝试了以下内容,但我有错误,知道吗? 我发现了这个错误,你知道怎么解决吗: 方法的类型参数 'Web.Cont

我有下面的类,我有两个对象,一个是Obj1,一个是以前的数据,另一个是obj2,数据中有一些可以更改的字段(我正在编辑,在更改前有Obj1,在更改后有obj2)。我的问题是,如果我有两个对象,如何最好地放入对象(如键val列表)只是更改的字段,它们具有值。 我在SO上读到过这篇文章,我发现了这两种方法,但有些帖子是旧的。。。, 什么是正确/有效的方法?示例将非常有用

我在这篇文章中尝试了以下内容,但我有错误,知道吗?

我发现了这个错误,你知道怎么解决吗:

方法的类型参数
'Web.Controllers.extensions.DetailedCompare(T,T)
不能 从用法推断。尝试显式指定类型参数


您必须创建一个函数,该函数列出并比较两个对象的所有属性。这可以通过反射来实现:

public class UserData
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string Work { get; set; }
    public string Home { get; set; }

    public IEnumerable<PropertyInfo> GetVariance(UserData user)
    {
        foreach (PropertyInfo pi in user.GetType().GetProperties()) {

            object valueUser = typeof(UserData).GetProperty (pi.Name).GetValue (user);
            object valueThis = typeof(UserData).GetProperty (pi.Name).GetValue (this);

            if (valueUser != null && !valueUser.Equals(valueThis))
                yield return pi;

        }
    }

}
公共类用户数据
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
公共字符串地址{get;set;}
公共字符串电子邮件{get;set;}
公共字符串工作{get;set;}
公共字符串Home{get;set;}
公共IEnumerable GetVariance(用户数据用户)
{
foreach(user.GetType().GetProperties()中的PropertyInfo pi){
objectvalueuser=typeof(UserData).GetProperty(pi.Name).GetValue(user);
对象值this=typeof(UserData).GetProperty(pi.Name).GetValue(this);
if(valueUser!=null&!valueUser.Equals(valueThis))
收益率;
}
}
}
我使用“Equals方法”来比较字符串、int等的值,而不是它们的引用与“==”(这里比较引用,因为我们得到的是对象类型)

UserData obj1=newuserdata();
obj1.Email=“www.test.com”;
obj1.Home=“测试主页”;
UserData obj2=新的UserData();
obj2.Email=“www.test2.com”;
obj2.Home=“测试home2”;
IEnumerable variances=obj1.getvariances(obj2);
foreach(以方差表示的属性信息pi)
Console.WriteLine(pi.Name);

注意,它只适用于原语的类型(int、string、float等),因为Equals比较两个对象的引用。

@patrickhoffman不确定我是否得到了它……什么是
obj1 email=“www.test.com”
mean?@KonradKokosa这只是从previos更改的对象属性的示例…修复我的帖子,并将第二个更改为obj2…@Patrickhoffman-什么是不编译?在我的示例中,有6个属性,其中只有两个(主页、电子邮件)发生了更改,我想获取它们,如何操作?我尝试使用底部的示例,但没有成功。这意味着您访问
属性的方式在语法上不正确。正确的方法是使用
而不是
-
。比如:
obj2.email
not
obj2 email
多谢大家投票!,最后一个问题是,我所有的属性都是另一个对象中两个属性旁边的字符串,我应该如何使用reflecation获取它们?如果这些属性的类型是您定义的类,您可以重写“Equals”或查看此链接,否则会更复杂。。。不幸的是,我不知道什么是最好的方法。
changedList = obj1.DetailedCompare(obj2);
public class UserData
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string Work { get; set; }
    public string Home { get; set; }

    public IEnumerable<PropertyInfo> GetVariance(UserData user)
    {
        foreach (PropertyInfo pi in user.GetType().GetProperties()) {

            object valueUser = typeof(UserData).GetProperty (pi.Name).GetValue (user);
            object valueThis = typeof(UserData).GetProperty (pi.Name).GetValue (this);

            if (valueUser != null && !valueUser.Equals(valueThis))
                yield return pi;

        }
    }

}
        UserData obj1 = new UserData();

        obj1.Email = "www.test.com";
        obj1.Home = "test home";
        UserData obj2 = new UserData();

        obj2.Email = "www.test2.com";
        obj2.Home = "test home2";

        IEnumerable<PropertyInfo> variances = obj1.GetVariance (obj2);

        foreach (PropertyInfo pi in variances)
            Console.WriteLine (pi.Name);