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!");
}
InvokeMethodisn’t used byGetMethod, but it is used when you pass aBindingFlagstoType.InvokeMember.BindingFlagsis a strange kind of enum that combines three separate pieces of functionality (according to MSDN, ‘accessibility’, ‘binding argument’ and ‘operation’). These three pieces of functionality don’t make sense wherever aBindingFlagsparameter is needed.