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<T>() {
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;
}
}
An extension method called HasAdditionOp
like this:
pubilc static bool HasAdditionOp(this Type t)
{
var op_add = t.GetMethod("op_Addition");
return op_add != null && op_add.IsSpecialName;
}
Note that IsSpecialName
prevents a normal method with the name "op_Addition";
You should check if class have method with op_Addition
name
You can find overloaded method names here,
Hope this helps