Consider this admittedly contrived Generic Definition:
private void Foo<T,BASETYPE>(PropertyInfo prop, BASETYPE o1, BASETYPE o2)
{
T value1 = (T) prop.GetValue(o1, null);
T value2 = (T) prop.GetValue(o2, null);
if (value1 != value2)
Console.WriteLine("NOT EQUAL");
}
prop is guaranteed to be a PropertyInfo for BASETYPE.
I am getting a compile error at the if() statement:
Operator '!=' cannot be applied to operands of type 'T' and 'T'
While in the “general case” I understand that the error message is valid, in this case, I only want the routine for some of the standard types: System.Int64, System.String, etc all of which support the == and != operator.
I assume this can be fixed with a “where” clause, but IComparable and IEqualable don’t help.
Do anyone know what the correct “where” clause is?
Frank
Since System.Int64, System.String, etc .. from your list implement
IComparable, you could useand use
CompareTo()instead of!=For eg. this code would compile