문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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)});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top