Search code examples
c#reflectionmethodinfo

Method Info is returning null, uncertain to why


sealed public class HMethod
{
    public int Calc(string Method, int X1, int X2, int Y1, int Y2)
    {
        MethodInfo HMethodInfo = this.GetType().GetMethod(Method);
        return (int)HMethodInfo.Invoke(
            this, 
            new object[4] { X1, X2, Y1, Y2 }
            );
    }
    int ManhattanH(int X1, int X2, int Y1, int Y2)
    {
        //Blah
    }
    int LineH(int X1, int X2, int Y1, int Y2)
    {
        //Blah
    }
    //Other Heuristics
}

When calling new HMethod().Calc("ManhattanH". X1, X2, Y1, Y2) HMethodInfo is null. Creates a null reference Exception. It should call the Method passed in via text (Which is grabbed from a text file)

Resolved: Methods are private.


Solution

  • ManhattanH is private method. Make this method is public or use BindingFlags.NonPublic.