How to verify whether a type overloads/supports a certain operator?

后端 未结 3 1359
孤城傲影
孤城傲影 2020-12-10 15:18

How can I check whether a certain type implements a certain operator?

struct CustomOperatorsClass
{
    public int Value { get; private set; }


    public C         


        
3条回答
  •  北海茫月
    2020-12-10 16:08

    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";

提交回复
热议问题