Reflection: invoke method on a form using its handle
1. I use the following to invoke a method on a form:
private void InvokeMethod(Form form, string methodName, params object[] parms)
{
try
{
EventHandler eh = (EventHandler)Delegate.CreateDelegate(typeof
(EventHandler), form, methodName);
if (eh != null)
{
form.Invoke(eh, parms);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
2. The prerequisite for #1 is:
try
{
testAssembly = Assembly.LoadFrom(textBox3.Text);
Type t = testAssembly.GetType(textBox4.Text);
testForm = (Form)testAssembly.CreateInstance(t.FullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(RunApp), testForm);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
3. What I need is invoking a method on a form that is running, i.e. skipping #2. I thought I could retrieve the handle to the running form, then by some cast invoke the wanted method. However, I am stuck. I have no idea how to go from a form's handle back to the form.
4. Please help.