Possible Duplicate:
Operator Overloading with C# Extension Methods
How can i overload those operators, i feel misunderstood by the compiler.
I think the core problem is that i try to overload an operator as an extension to a class. The type class does not have those operators, so i feel pretty safe in doing so – but my compiler disagrees wildly.
public static class TypeCheck
{
public static Boolean ToBool(this Type t1, Type t2)
{
//normal extension works
return true;
}
public static Boolean operator > (this Type t1, Type t2)
{
//TODO once it compiles
return fasle;
}
public static Boolean operator < (this Type t1, Type t2)
{
//TODO once it compiles
return true;
}
}
To clarify the domain specifics of those comparisons: class A : B {}, class B {} and class C {} A is greater than A and greater than B but smaller than the rest. Because A.IsCastableTo(B) and A.IsCastableTo(A);
Like the others have said, C# doesn’t support extension operators. If you need to do this then you can implement a custom type, something like:
Alternatively it would probably be easier to implement a custom
DynamicObject(C# 4+) which can delegate the calls.