Question

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.

Was it helpful?

Solution

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

OTHER TIPS

GetMethod automatically searches only for public members of that type. You can get around this (and have the search include private members) by substituting in this line:

MethodInfo HMethodInfo = this.GetType().GetMethod(Method, BindingFlags.Instance | BindingFlags.NonPublic);

Type.GetMethod Method (String, Type[])

The search for name is case-sensitive. The search includes public static and public instance methods.

You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.

Change your method to public and try this:

MethodInfo HMethodInfo = this.GetType().GetMethod(Method,
    new Type[]{typeof(int), typeof(int), typeof(int), typeof(int)});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top