How can I check whether a certain type implements a certain operator?
struct CustomOperatorsClass
{
public int Value { get; private set; }
public CustomOperatorsClass( int value )
: this()
{
Value = value;
}
static public CustomOperatorsClass operator +(
CustomOperatorsClass a, CustomOperatorsClass b )
{
return new CustomOperatorsClass( a.Value + b.Value );
}
}
Following two checks should return true:
typeof( CustomOperatorsClass ).HasOperator( Operator.Addition )
typeof( int ).HasOperator( Operator.Addition )
There is a quick and dirty way to find out, and it works for both built-in and custom types. Its major drawback is that it relies on exceptions in a normal flow, but it gets the job done.