How do I use reflection to invoke a private method?

后端 未结 10 668
说谎
说谎 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:47

    BindingFlags.NonPublic will not return any results by itself. As it turns out, combining it with BindingFlags.Instance does the trick.

    MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType, 
        BindingFlags.NonPublic | BindingFlags.Instance);
    

提交回复
热议问题