C# 自定义类的Type.GetMethod()返回NULL

C# 自定义类的Type.GetMethod()返回NULL,c#,reflection,C#,Reflection,在使用type.GetMethod()指定比较类型时,我在获取我的MyClass==运算符的方法引用时遇到问题,下面是我的代码: public class MyClass { public object Value { get; set; } public MyClass(object inVal = null) { Value = inVal; } public static bool operator ==(MyClass a, s

在使用
type.GetMethod()
指定比较类型时,我在获取我的
MyClass
==运算符的方法引用时遇到问题,下面是我的代码:

public class MyClass
{
    public object Value { get; set; }

    public MyClass(object inVal = null)
    {
        Value = inVal;
    }

    public static bool operator ==(MyClass a, string b)
    {
        // If one is null, but not both, return false.
        if (((object)a == null) || ((object)b == null)) return false;

        // Return true if the fields match:
        return Convert.ToString(a.Value) == b;
    }

    public static bool operator !=(MyClass a, string b)
    {
        return !(a == b);
    }

    public static bool operator ==(MyClass a, bool b)
    {
        // If one is null, but not both, return false.
        if ((object)a == null) return false;

        // Return true if the fields match:
        return Convert.ToBoolean(a.Value) == b;
    }

    public static bool operator !=(MyClass a, bool b)
    {
        return !(a == b);
    }
}
打电话给

var methodInfo = typeof(MyClass).GetMethod("op_Equality", new Type[]  {  typeof(bool)  } )


,返回
NULL
,这是为什么?我需要一个对操作符的引用。

普通方法需要一个公共实例方法来查找。由于您的方法是静态方法,因此应该使用
GetType
重载,您可以传递
BindingFlags
参数
static

普通方法需要公共实例方法来查找。由于您的方法是静态方法,因此应该使用
GetType
重载,您可以传递
BindingFlags
参数
static

普通方法需要公共实例方法来查找。由于您的方法是静态方法,因此应该使用
GetType
重载,您可以传递
BindingFlags
参数
static

普通方法需要公共实例方法来查找。由于您的方法是静态方法,因此应该使用
GetType
重载,您可以传递
static

BindingFlags
参数,等式/不等式运算符适用于两种类型,
(在您的例子中是类类型和bool/string类型), 您还需要传递类类型(按照预期的正确顺序)


等式/不等式运算符适用于两种类型,
(在您的例子中是类类型和bool/string类型), 您还需要传递类类型(按照预期的正确顺序)


等式/不等式运算符适用于两种类型,
(在您的例子中是类类型和bool/string类型), 您还需要传递类类型(按照预期的正确顺序)


等式/不等式运算符适用于两种类型,
(在您的例子中是类类型和bool/string类型), 您还需要传递类类型(按照预期的正确顺序)


这不是问题。错误的类型是问题所在,请参阅公认的答案。这不是问题所在。错误的类型是问题所在,请参阅公认的答案。这不是问题所在。错误的类型是问题所在,请参阅公认的答案。这不是问题所在。错误的类型是问题所在,请参阅公认的答案。
var methodInfo = typeof(MyClass).GetMethod("op_Equality", new Type[]  {  typeof(string)  } )
Type t1 = typeof(MyClass);
var methodInfo1 = t1.GetMethod("op_Equality", 
                  new Type[]  { t1, typeof(bool)  } );
var methodInfo2 = t1.GetMethod("op_Equality", 
                  new Type[]  { t1, typeof(string)  } );