How can I check whether a certain type implements a certain operator?
struct CustomOperatorsClass
{
public int Value { get; private set; }
public C
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.
static bool HasAdd() {
var c = Expression.Constant(default(T), typeof(T));
try {
Expression.Add(c, c); // Throws an exception if + is not defined
return true;
} catch {
return false;
}
}