I am fetching data from database in an object and updating the same object with new values. Now I want to check which properties the same object are updated. How to check it?
I tried using creating a new object and passing the old object in new one. But after updating the old one, I found the new one has the same value.
The sample code is as below :
public class Patient
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
Patient p = new Patient();
p.Id = 1;
p.FirstName = "k";
p.LastName = "p";
Patient copy = new Patient();
copy = (Patient)p;
p.FirstName = "d";
p.LastName = "g";
p.Id = 2;
foreach (PropertyInfo pr in p.GetType().GetProperties())
{
string result = string.Empty;
string currentValue = copy.GetType().GetProperties().Single(o => o.Name == pr.Name).GetValue(copy, null).ToString();
string pastValue = pr.GetValue(p, null).ToString();
if (currentValue == pastValue)
{
result = "equal" ; // I found that the values of properties of both objects are always same
}
else
{
result = "different"; // I want this result
}
}
}
Please let me know how can I compare old object property value with new one?