Welcome on Kweetix Technical Blog

Monday, September 21, 2009

Call a method in another assembly with C#

// Load the assembly

Assembly assembly = Assembly.LoadFrom(@"C:\App\bin\xyz.dll");

// Create the actual type

Type classType = assembly.GetType("xyz", true, false);

// Create an instance of this type and verify that it exists

object dynamicObject = Activator.CreateInstance(classType);

if (dynamicObject != null)
{
// Verify that the method exists and get its MethodInfo object

MethodInfo invokedMethod = classType.GetMethod("Start");

if (invokedMethod != null)
{
object[] parameters = new object[2];
parameters[0] = jobType;
parameters[1] = argument;

// Invoke the method with the parameters

invokedMethod.Invoke(dynamicObject, parameters);
}
}

No comments:

Post a Comment