C# структура двух классов - PullRequest
0 голосов
/ 21 апреля 2020

Как сравнить и получить различия между двумя классами.

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;}

Ответы [ 2 ]

1 голос
/ 21 апреля 2020

Предположим, у вас есть эти два класса:

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 - нет.
Идентификатор имеет тип «System.Int32» в ClassA и тип «System.String» в ClassB

0 голосов
/ 21 апреля 2020

Вы можете получить список свойств / типов каждого класса

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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...