Loading assemblies by Reflection

An assembly contains metadata and the IL representation of an application. To use an assembly dynamically we can use Load or LoadForm static methods on the assembly class. The Assembly.LoadForm is an easiest way to load an assembly at the run-time.  We can load the assembly using 7 variations using Assembly.Load method.

 

Check the below snippet which demonstrates how to construct and initialize an AssemblyName object.

 

 

Assembly.LoadForm method

 

Assembly  assembly = Assembly.LoadForm(“C:\\WINNT\\Microsoft.NET\\Framework\\V1.0.3705\\System.Windows.Forms.dll”);

 

foreach(Type type in assembly.GetTypes())

{

 

Console.WriteLine(type.FullName);

 

}

 

Above code loads an assembly by providing the complete file path to the System.Windows.Forms.dll assembly. The above way is the simplest way to load an assembly.

 

AssemblyName object

 

AssemblyName assemblyName = new AssemblyName();

 

assemblyName.CodeBase = file:///C:\\WINNT\\Microsoft.NET\\Framework\\V1.0.3705\\System.Windows.Forms.dll;

 

assemblyName.Version = new Version (“1.0.3300.0”);

assemblyName.CultureInfo = new CultureInfo (“en-US”);

 

Assembly assembly =  Assembly.Load(assemblyName);

 

foreach(Type type in assembly.GetTypes() )

{

 

Console.WriteLine(“type.FullName”)

 

}

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish