Dynamically loading a dll and invoking a method in it

Language: C#

Source Code:

// Adding assembly referenece dynamically

Assembly addassembly = Assembly.LoadFrom(<path>);

foreach (Type type in addassembly.GetTypes())

{

// If it is class it will go into the “if” structure

if (type.IsClass == true)

{

// the class must have extended an interface which has the method name that you want to invoke, this ensures the presence of the method that you want to invoke in the

if (type.GetInterface(<Inteface_name>) == null)

{

continue;

}

object obj = Activator.CreateInstance(type);

// Invokes the method

object result = type.InvokeMember(<method_name>, BindingFlags.InvokeMethod | BindingFlags.Default, null, obj, null);

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s