CSharp examples for System:Type
test if two objects are equal field by field (with deep inspection of each field)
using System.Reflection; using System.Text; using System.Linq; using System.Collections.Generic; using System;/*from w w w.ja v a 2 s . c o m*/ public class Main{ /// <summary> /// test if two objects are equal field by field (with deep inspection of each field) /// </summary> /// <param name="o1"></param> /// <param name="o2"></param> /// <returns></returns> public static bool DeepEquals(object o1, object o2) { if (o1 == o2) { // reference equal, so nothing else to be done return true; } Type t1 = o1.GetType(); Type t2 = o2.GetType(); if (t1 != t2) { return false; } if (t1.IsArray) { // it's not too hard to support thesse if needed if (t1.GetArrayRank() > 1 || IsNonZeroBaedArray(t1)) throw new InvalidOperationException("Complex arrays not supported"); // this is actually pretty fast; it allows using fast ldelem and stelem opcodes on // arbitrary array types without emitting custom IL var method = ArrayEqualsGeneric.MakeGenericMethod(new Type[] { t1.GetElementType() }); return (bool)method.Invoke(null, new object[] { o1, o2 }); } else if (t1.IsPrimitive) { return o1.Equals(o2); } else { foreach (var field in GetAllFields(t1)) { if (!DeepEquals(field.GetValue(o1), field.GetValue(o2))) return false; } return true; } } }