Desidero ottenere i nomi di tutte le proprietà modificate per gli oggetti corrispondenti. Ho questi (semplificato) classi:Confronta proprietà automaticamente
public enum PersonType { Student, Professor, Employee }
class Person {
public string Name { get; set; }
public PersonType Type { get; set; }
}
class Student : Person {
public string MatriculationNumber { get; set; }
}
class Subject {
public string Name { get; set; }
public int WeeklyHours { get; set; }
}
class Professor : Person {
public List<Subject> Subjects { get; set; }
}
Ora voglio ottenere gli oggetti in cui i valori delle proprietà differiscono:
List<Person> oldPersonList = ...
List<Person> newPersonList = ...
List<Difference> = GetDifferences(oldPersonList, newPersonList);
public List<Difference> GetDifferences(List<Person> oldP, List<Person> newP) {
//how to check the properties without casting and checking
//for each type and individual property??
//can this be done with Reflection even in Lists??
}
Alla fine mi piacerebbe avere una lista di Difference
s come questo:
class Difference {
public List<string> ChangedProperties { get; set; }
public Person NewPerson { get; set; }
public Person OldPerson { get; set; }
}
il ChangedProperties
deve contenere il nome delle proprietà modificate.
Fare questo per gli elenchi è un dolore * reale * (supponendo che sia necessario gestire aggiungere/rimuovere/riordinare/etc); tuttavia, in base all'oggetto, vedere: http://stackoverflow.com/questions/3060382/comparing-2-objects-and-retrieve-a-list-of-fields-with-different-values - che fa esattamente questo –
@MarcGravell: l'ho provato e restituisce gli attributi che sono liste come delta. Grazie comunque. –
Ti interessano le proprietà che non sono in entrambi gli oggetti, I.e. dovrebbe matriculationNumber essere considerato un cambiamento quando si confronta una persona con uno studente? –