Load Assemblies Dynamically

The code sample contains a Function used to Load an assembly dynamically. To do so, the static Assmebly.LoadFile() method is called. To it is passed the path to the assembly we wish to load. The method returns a reference to the Assembly.

An Assembly has methods such as GetType with which class types in the assembly can obtained.


private object LoadAssembly()
{
Assembly objm = Assembly.LoadFile("Load Your Dll (Dll Name with Path)");
//NameSpace With Class Name
Type objmycom = objm.GetType("NameSpace.ClassName");
//Bind The Method
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
//GetMethod
MethodInfo m = objmycom.GetMethod("Method Name", flags);
//Create Instance For Dll
object myobj = Activator.CreateInstance(objmycom, false);
//Pass The Value as Object
object[] param = {int.Parse(txtFirstNo.Text),int.Parse(txtSecondNo.Text)};
object Z = m.Invoke(myobj, BindingFlags.InvokeMethod, null, param, System.Globalization.CultureInfo.CurrentCulture);
return Z;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: