Better late than never, as they say.
Not as elegant as Dan's, but here is my take.
In a simple Windows Form app with one textbox (default name) and 1 button (default name), it will invoke a method in the Form class or in another class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string Input = textBox1.Text;
TestClass testClass = new TestClass();
Type testClassTypes = testClass.GetType();
Type thisTypes = this.GetType();
System.Reflection.MethodInfo[] testClassInfo = testClassTypes.GetMethods();
System.Reflection.MethodInfo[] thisInfo = thisTypes.GetMethods();
foreach (MethodInfo newInfo in testClassInfo)
{
if (newInfo.Name == Input)
{
string returnedData = (string)newInfo.Invoke(testClass, null);
MessageBox.Show(returnedData);
}
}
foreach (MethodInfo newInfo in thisInfo)
{
if (newInfo.Name == Input)
{
string returnedData = (string)newInfo.Invoke(this, null);
MessageBox.Show(returnedData);
}
}
}
public string Test1()
{
return "Test1";
}
public string Test2()
{
return "Test2";
}
public class TestClass
{
public string Test1()
{
return "Test1 from TestClass1";
}
public string Test2()
{
return "Test2 from TestClass2";
}
}
}
}