Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
C#
System.Reflection.Assembly asm;  
asm = System.Reflection.Assembly.LoadFrom("Test2.exe"); // get from different project but same application
var forms = (Form)Activator.CreateInstance(asm.GetType("Test2.Form1")); // call form
forms.Show(); // show form


Can I call Winform without using CreateInstance
As Create Instance is very slow

What I have tried:

I trying to use System.Reflection.Emit

but in the end still need to use CreateInstance

DynamicMethod method = new DynamicMethod("CreateInstance", Type.GetType("testing.Form2"), new Type[] { typeof(object[]) }, true);
(Form)Activator.CreateInstance(method);
Posted
Updated 12-Jul-16 16:53pm

Object[] args = new Object[] { };

var forms1 = (Form)Type.GetType("testing.Form2").InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
forms1.Show();
Not sure any different from (Form)Activator.CreateInstance(method) but it work
 
Share this answer
 
yes, in the end, you still need to use CreateInstance

... that doesnt mean you cant be smarter about when you do this though - if you knew you were going to load 'testing.Form2' from 'Test2.Exe' when your program loaded, then, you could 'bootstrap' the process in a background thread while your main Winform project is loading (much like a plug-in system loading its plugins from dlls in a directory would)
 
Share this answer
 
Comments
Beginner Luck 4-Jul-16 21:24pm    
I saw ILGenerator but do it also need CreateInstance ??
Garth J Lancaster 5-Jul-16 0:04am    
I dont know sorry, it *may* be possible, but faster ? try both methods and compare them

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900