Rohit Raghuvansi
Rohit Raghuvansi

Reputation: 2864

Find changed properties of a class

I am using asp.net 3.5

I have a license class containing 10 properties and not marked as serializable. I have to log property changes to the database. License is an entity class,and it is not in my scope to modify it. So I cannot mark it as serializabel neither i can use IpropertyChanged Interface.

Now i cannot store it in viewstate as it is not serializable. I wanted to store it so that i can compare it with new values and see which value has changed.

How to proceed with this.

Upvotes: 1

Views: 2045

Answers (3)

Jasper
Jasper

Reputation: 451

As an alternative to the given answers you could have a look at Aspect Oriented Programming (for example PostSharp: http://www.sharpcrafters.com/ ).

This way you can create an method outside the class that gets called each time a property gets set. After a diff any changes can be inserted into the database

Upvotes: 0

lxalln
lxalln

Reputation: 930

If you have the original object (ie. before it was changed). Then it might be best to use reflection to loop through the exposed properties and compare them.

Your code will probably end up looking something like this (this isn't a full sample):

Type type = license.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach(PropertyInfo property in properties)
{
//compare your properties with property.GetValue(newLicense, null) and property.GetValue(originalLicense, null);
}

Then you can store, or do whatever you want with the different columns.

Upvotes: 1

Martin Milan
Martin Milan

Reputation: 6390

You could do this by writing a class that looks at a target object and uses reflection to store the value of all it's exposed properties in a Name / Value dictionary. Then, at "Compare" time, you use reflection again and compare to the values stored in your dictionary?

Just a guess...

Upvotes: 2

Related Questions