C#压缩两类结构



如何比较并获得两个类之间的差异。

public class Class1{
public string S1{get;set;}
public string S2{get;set;}
public Class2 C3{get;set;}
}
public class Class2{
public string S1{get;set;}
public string S2{get;set;} 
}
public class Class3{
public string S1{get;set;}
public string S2{get;set;}
public string S12{get;set;}
public Class4 C3{get;set;}
}
public class Class4{
public string S1{get;set;}
public string S2{get;set;}
public string S112{get;set;} 
}

最后,我想知道是否缺少创建类实例的成员

public string S112{get;set;}   

public string S12{get;set;}

假设您有以下两个类:

public class ClassA
{
public int Id { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
}
public class ClassB
{
public string Id { get; set; }
public string Description { get; set; }
public string CreatedBy { get; set; }
}

只检索那些在该类中定义的属性(而不是继承的(:

public static Dictionary<string, PropertyInfo> GetProperties<TClass>() where TClass : class
{
return typeof(TClass)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.ToDictionary(prop => prop.Name);
}

找出缺失的:

public static IEnumerable<string> GetMissingProperties(string lhsName, IList<string> lhsPropertyNames, string rhsName, IList<string> rhsPropertyNames)
{
var lhsMissingOnes = lhsPropertyNames.Except(rhsPropertyNames).Select(prop => lhsName + " defines " + prop + " but " + rhsName + " does not.");
var rhsMissingOnes = rhsPropertyNames.Except(lhsPropertyNames).Select(prop => rhsName + " defines " + prop + " but " + lhsName + " does not.");
return lhsMissingOnes.Union(rhsMissingOnes);
}

找出不同类型的:

public static IEnumerable<string> GetDifferentlyTypedProperties(string lhsName, Dictionary<string, PropertyInfo> lhsProperties, string rhsName, Dictionary<string, PropertyInfo> rhsProperties)
{
var differentTypes = new List<string>();
var definedInBoth = lhsProperties.Keys.Intersect(rhsProperties.Keys);
foreach (var prop in definedInBoth)
{
var lhsType = lhsProperties[prop].PropertyType;
var rhsType = rhsProperties[prop].PropertyType;
if (lhsType != rhsType)
{
var message = prop + " has a type '" + lhsType + "' in " + lhsName + " and '" + rhsType + "' type in " + rhsName;
differentTypes.Add(message);
}
}
return differentTypes;
}

把所有东西放在一起:

public static void Main()
{
var propertiesOfA = GetProperties<ClassA>();
var propertiesOfB = GetProperties<ClassB>();
var missingProperties = GetMissingProperties(nameof(ClassA), propertiesOfA.Keys.ToList(), nameof(ClassB), propertiesOfB.Keys.ToList());
foreach (var message in missingProperties) Console.WriteLine(message);
var differentProperties = GetDifferentlyTypedProperties(nameof(ClassA), propertiesOfA, nameof(ClassB), propertiesOfB);
foreach (var message in differentProperties) Console.WriteLine(message);
}

输出将是:
ClassA定义CreatedAt,而ClassB没有
ClassB定义CreatedBy,但ClassA没有
Id在ClassA中具有"System.Int32"类型,在ClassB中具有"System.String"类型

您可以获得每个类的属性/类型列表

var propNameType1 = typeof(Class1).GetProperties()
.Select(prop => new  {
name = prop.Name,
type = prop.PropertyType.Name
} )
.ToList();
var propNameType2 = typeof(Class2).GetProperties()
.Select(prop => new {
name = prop.Name,
type = prop.PropertyType.Name
})
.ToList();

然后你可以将列表与LINQ 进行比较

var differences = propNameType1.Except(propNameType2).ToList();

最新更新