Compare objects

F

Frank

I have 2 objects that have the same complex structure and I would like
to know if a member has change and which one(s).

For example:

[Serializable]
public struct Person
{
public long m_nID;
public string m_strPrenom;
public string m_strNom;
}

If I have p1 and p2 which are Person, I would like a method like
p1.theMethod(p2) that returns an array of objects with the member(s)
and the value(s) where the values are different.

My structure is big, so I don't know if any method already exists in
dotnet?

Thanks,
Francois
 
G

Guest

Hi Frank,

I propose You to write additional structure e.g.

public struct PersonDetailDifference {
public int IdDifference;

public int PrenomDifference;

public int NomDifference;

// and continue...
}

Results of each fields should correspond with
IComparable.Compare(...) results of two Person
field values.

Person should have a constant structure so i
propose to use another strucure as a result
of comparison.

The good practise is to use the properties
instead of fieds for public access.

Regards

Marcin
 
N

Niki Estner

There is no "out-ouf-the-box" solution for this (to the best of my
knowledge).
But you can automate this task using reflection: Get the metadata of your
type, loop over it's fields/properties and compare each of them, put the
different ones in an array.

Look at Object.GetType; Type.GetFields/GetProperties, FieldInfo.GetValue,
PropertyInfo.GetValue.

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top