How do I use reflection to invoke a private method?

后端 未结 10 793
说谎
说谎 2020-11-22 14:05

There are a group of private methods in my class, and I need to call one dynamically based on an input value. Both the invoking code and the target methods are in the same i

10条回答
  •  抹茶落季
    2020-11-22 14:24

    Simply change your code to use the overloaded version of GetMethod that accepts BindingFlags:

    MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType, 
        BindingFlags.NonPublic | BindingFlags.Instance);
    dynMethod.Invoke(this, new object[] { methodParams });
    

    Here's the BindingFlags enumeration documentation.

提交回复
热议问题