G
GeezerButler
I am comparing 2 objects with a lot of public properties(more than 100
which are all ValueType or string)
So i decided to write a reflection based solution for that.
My code looks something like this:
public bool AreEqual(Mytype a, Mytype b)
{
PropertyInfo[] props =
typeof(MyType).GetProperties(PUBLIC_PROP_BINDINGS);
foreach (PropertyInfo prop in props)
{
object objA = prop.GetValue(a, null);
object objB = prop.GetValue(b, null);
switch (prop.PropertyType.Name)
{
case "Int32":
if ((int)objA != (int)objB) return false; break;
case "String":
if ((string)objA != (string)objB) return false; break;
case "DateTime":
if ((DateTime)objA != (DateTime)objB) return false; break;
}
}
return true;
}
This reduces the code somewhat but can i somehow avoid using the large
amount of case "typeName" statements.
I am having to do this because objA != objB is always true unless the
objects are cast to their correct type.
which are all ValueType or string)
So i decided to write a reflection based solution for that.
My code looks something like this:
public bool AreEqual(Mytype a, Mytype b)
{
PropertyInfo[] props =
typeof(MyType).GetProperties(PUBLIC_PROP_BINDINGS);
foreach (PropertyInfo prop in props)
{
object objA = prop.GetValue(a, null);
object objB = prop.GetValue(b, null);
switch (prop.PropertyType.Name)
{
case "Int32":
if ((int)objA != (int)objB) return false; break;
case "String":
if ((string)objA != (string)objB) return false; break;
case "DateTime":
if ((DateTime)objA != (DateTime)objB) return false; break;
}
}
return true;
}
This reduces the code somewhat but can i somehow avoid using the large
amount of case "typeName" statements.
I am having to do this because objA != objB is always true unless the
objects are cast to their correct type.