relational operator on basic type

  • Thread starter Thread starter Locia
  • Start date Start date
L

Locia

I would like compare all type.
I try with this function but I get System.NullReference.Exception if
pass in Compare function two int type.

Why relational operation isn't defined for basic type as int?
How can I do?

private bool Compare(Object leftOperand,String op,Object rightOperand)
{
if (op.Equals(">"))
{
Type typeObject=leftOperand.GetType();
MethodInfo method = typeObject.GetMethod
("op_GreaterThan",BindingFlags.Public | BindingFlags.Static);
bool result=(bool) method.Invoke(leftOperand,objParam);
return result;
}
}
 
An explanation could be that int (.NET Int32) is a value type... it is
boxed/unboxed automatically and the comparison operator is directly
mapped to the cgt opcode so it does not need to override the relational
operator (op_GreaterThan is the overloaded operator method name).

I think that this is the answer...
 
Back
Top