I have the following code used to avoid a switch
statement to decide which method to invoke, and it works with just the BindingFlags flags I have set, without InvokeMethod
. What is InvokeMethod
actually meant for and why is it not needed in the following code:
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!");
}