What i'm trying to achieve here is a straight value comparison of boxed primitive types.
((object)12).Equals((object)12); // Type match will result in a value comparison,
((object)12).Equals((object)12d); // but a type mismatch will not. (false)
object.Equals((object)12,(object)12d); // Same here. (false)
I understand the 'why'. I just don't see a 'how'.
The types are unknown until runtime, where they could be any primitive type from a datasource. That includes strings, datetimes, bools, etc. I've gone down the ugly route of writing an extension method that works out both types, and then casts before doing a '==' comparison: (For completeness, i included every primitive type, plus those i was interested in)
public static bool ValueEquals(this object thisObj, object compare)
{
if (thisObj is int)
{
int obj = (int)thisObj;
if (compare is int)
return (obj == (int)compare);
if (compare is uint)
return (obj == (uint)compare);
if (compare is decimal)
return (obj == (decimal)compare);
if (compare is float)
return (obj == (float)compare);
<... and so on for each primitive type ...>
}
if (thisObj is uint)
{
uint obj = (uint)thisObj;
if (compare is int)
return (obj == (int)compare);
if (compare is uint)
return (obj == (uint)compare);
<... Again for each primitive type ...>
}
if (thisObj is decimal)
{
decimal obj = (decimal)thisObj;
if (compare is int)
return (obj == (int)compare);
<... Etc, etc ...>
The resulting method turned out to be 300+ lines long, which was fine (yet hideous), but now I need to do more than just '=='. I need >, <, <=, >=, !=.
Is there anything in Reflection that i could use for boxed value type comparisons?
Anything at all?
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.
The C# Type System contains three data types: Value Types (int, char, etc), Reference Types (object) and Pointer Types. Basically, Boxing converts a Value Type variable into a Reference Type variable, and Unboxing achieves the vice-versa.
Looks like you are assuming the type from arg1 is the one you want to convert to, so I'd use a genric like this. As long as arg2 is IConvertible (int, double, all numerics, string, etc are all IConvertible) this will work:
public static bool ValueEquality<T1, T2>(T1 val1, T2 val2)
where T1 : IConvertible
where T2 : IConvertible
{
// convert val2 to type of val1.
T1 boxed2 = (T1) Convert.ChangeType(val2, typeof (T1));
// compare now that same type.
return val1.Equals(boxed2);
}
** UPDATE ** Made both types generic args, can both be inferred and adds more compile time safety on arg2 to make sure it's IConvertible at compile time.
Given this generic function, all of the following now return true (don't need to specify type argument since inferred from first argument:
Console.WriteLine(ValueEquality(1, "1"));
Console.WriteLine(ValueEquality(2, 2.0));
Console.WriteLine(ValueEquality(3, 3L));
UPDATE
Based on your comment, here's an overload if all you have are objects. Both can co-exist and it will call the one more appropriate based on the arguments:
public static bool ValueEquality(object val1, object val2)
{
if (!(val1 is IConvertible)) throw new ArgumentException("val1 must be IConvertible type");
if (!(val2 is IConvertible)) throw new ArgumentException("val2 must be IConvertible type");
// convert val2 to type of val1.
var converted2 = Convert.ChangeType(val2, val1.GetType());
// compare now that same type.
return val1.Equals(converted2);
}
And this will work for object:
object obj1 = 1;
object obj2 = 1.0;
Console.WriteLine(ValueEquality(obj1, obj2));
As I said, both of these can co-exist as overloads, so if you compare compatible IConvertible types directly it will use the generic, and if you just have boxed types as object, it will use the object overload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With