Created
January 14, 2018 16:07
-
-
Save leachdaniel/e7a397633a31695701305f7701cbdeee to your computer and use it in GitHub Desktop.
Dynamically Load an Assembly and call a method on it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace InterfaceCaller | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// get the assembly from the path in argument | |
var asm = Assembly.LoadFile(args[0]); | |
// get the type name | |
var type = asm.GetType(args[1]); | |
var runnable = Activator.CreateInstance(type); | |
if (runnable == null) throw new Exception("the type was not runnable"); | |
// call with args[2]' | |
var method = type.GetMethod(args[2]); | |
// method has no arguments | |
method.Invoke(runnable, new object[] { }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment