Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较两个类的基本方法?_C#_.net_Reflection - Fatal编程技术网

C# 如何比较两个类的基本方法?

C# 如何比较两个类的基本方法?,c#,.net,reflection,C#,.net,Reflection,我想通过比较另一个类来找出一个类中缺少的属性 public class User { public int UserID { get; set; } public string UserName { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class UserDTO { public string Us

我想通过比较另一个类来找出一个类中缺少的属性

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class UserDTO
{

    public string UserName { get; set; }
    public string FirstName { get; set; }

}

上面我应该得到像“UserID”这样的输出,UserDTO中缺少LastName属性。

使用反射获取属性,请参阅。然后比较两个属性列表以查找缺少的属性

var UserProperties = typeof(User).GetProperties().Select(p => p.Name);
var UserDTOProperties = typeof(UserDTO).GetProperties().Select(p => p.Name);

var missingProperties = UserProperties.Except(UserDTOProperties);
考虑到所有继承的属性也将出现在这些列表中,除非您指定了
BindingFlags。对于
GetProperties()
方法,请参阅

编辑

在评论和公共
字段中包含建议

public static IEnumerable<string> Diff(Type t1, Type t2)
{
    return t1.GetProperties().Select(p1 => new { Name = p1.Name, Type = p1.PropertyType })
            .Concat(t1.GetFields().Select(f1 => new { Name = f1.Name, Type = f1.FieldType }))
            .Except(t2.GetProperties().Select(p2 => new { Name = p2.Name, Type = p2.PropertyType })
                    .Concat(t2.GetFields().Select(f2 => new { Name = f2.Name, Type = f2.FieldType })))
            .Select(a => a.Name);
}
公共静态IEnumerable Diff(t1型,t2型)
{
返回t1.GetProperties().Select(p1=>new{Name=p1.Name,Type=p1.PropertyType})
.Concat(t1.GetFields().Select(f1=>new{Name=f1.Name,Type=f1.FieldType}))
.Except(t2.GetProperties().Select(p2=>new{Name=p2.Name,Type=p2.PropertyType})
.Concat(t2.GetFields().Select(f2=>new{Name=f2.Name,Type=f2.FieldType})))
.选择(a=>a.Name);
}

为什么它不将
UserID
输出为缺失?您遇到了什么问题?这很简单。是的,问题已更新。@RockySingh我编辑了您的标题。如果它看起来有误,请随时更新它。这是一个很好的解决方案+1.+1。您甚至可以将它包含在像
IEnumerable GetDiff()这样的通用方法中
如果需要重用它:)我会添加类型
。选择(p=>new{Name=p.Name,type=p.PropertyType});
。然后您会意识到属性是否具有相同的名称但不同的类型。这将杀死
除了()
功能,除非您使用重写的
Equals
GetHashCode
函数将匿名类型转换为正确的类型,或者为其提供特殊的
EqualityComparer
。默认情况下,它现在将执行
对象。Equals()
它比较对象引用而不是值,因此每个属性都将显示为不同的。@WillemDuncan否,匿名类型按其属性进行比较(如果所有属性的值相等,则任何同名类型都相等)。请尝试查看
bool b=new{a=1}.Equals(new{a=1});
public static IEnumerable<string> Diff(Type t1, Type t2)
{
    return t1.GetProperties().Select(p1 => new { Name = p1.Name, Type = p1.PropertyType })
            .Concat(t1.GetFields().Select(f1 => new { Name = f1.Name, Type = f1.FieldType }))
            .Except(t2.GetProperties().Select(p2 => new { Name = p2.Name, Type = p2.PropertyType })
                    .Concat(t2.GetFields().Select(f2 => new { Name = f2.Name, Type = f2.FieldType })))
            .Select(a => a.Name);
}