Reputation: 367
I have a dll that used to be a console program, it still has code to use "static Main(string[] args)" is there a way to pass args when I load the DLL?
I'm using
Assembly assembly = Assembly.LoadFrom("App.dll");
Type type = assembly.GetType("App.LoadFile");
var instance = Activator.CreateInstance(type,new string[] { filepath });
Upvotes: 4
Views: 107
Reputation: 34543
The Main
method will not be called just by loading the assembly in your 2nd program. You just need to invoke the Main
method on the type
you have above.
Here's how you can call the static Main
method from your code...
var assembly = Assembly.LoadFrom("App.dll");
var type = assembly.GetType("App.LoadFile");
var args = new string[] { filepath };
var main = type.GetMethod("Main",
System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.InvokeMethod
);
main.Invoke(null, new object[] { args });
Upvotes: 2