(Assuming you want to compare all fields of two objects for equality.)
As a rule, you would not use reflection for this, you would simply compare each field yourself. An IEquatable<T> interface exists for this purpose, and you can also override Object.Equals() for the types in question. For example:
public class DataTypeTwo : BaseData, IEquatable<DataTypeTwo> { public int CustId; public string CustName; public override int GetHashCode() { return CustId ^ CustName.GetHashCode();
Also, consider if your type makes sense as a struct . Value types automatically compare equality by conducting field comparisons.
Note that by overriding Equals , you basically achieve what (it seems to me) that you are trying to achieve using your equal method method with your scheme. That is, people using DataTypeTwo will naturally be able to test equality without knowing anything special about your API, they will just use Equals as if they were with other things.
EDIT: Thanks to Jared for reminding me of GetHashCode . You will also want to override it to maintain normal behavior in the hash tables, making sure that any two "equal" objects return the same hash code.
mquander
source share