BindingFlags.InvokeMethod 是什么意思?

Pro*_*ofK 4 .net c# reflection

我使用以下代码来避免使用switch语句来决定调用哪个方法,它仅适用于我设置的 BindingFlags 标志,没有InvokeMethod. 什么是InvokeMethod真正的意思,为什么它没有在下面的代码需要:

public enum PublishMethods
{
    Method1,
    Method2,
    Method3
}
private void Form1_Load(object sender, EventArgs e)
{
    InvokePublishMethod(PublishMethods.Method2);
}
private void InvokePublishMethod(PublishMethods publishMethod)
{
    var publishMethodsType = this.GetType();
    var method = publishMethodsType.GetMethod("Publish" + publishMethod, BindingFlags.NonPublic | BindingFlags.Instance);
    method.Invoke(this, null);
}
private void PublishMethod2()
{
    MessageBox.Show("Method2!");
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*son 5

InvokeMethod不使用GetMethod,但是当你传递一个用它BindingFlagsType.InvokeMember

BindingFlags是一种奇怪的枚举,它结合了三个独立的功能部分(根据 MSDN,“可访问性”、“绑定参数”和“操作”)。这三个功能在需要BindingFlags参数的地方没有意义。