In-depth explanation of Assembly.Load in C#

The Assembly.Load method is a method in the .NET Framework used to load assemblies. Its main purpose is to load the assembly into the current application domain based on the assembly’s name or path.

There are multiple overloaded versions of this method, and you can choose the appropriate version according to your needs when using it. Here is a detailed explanation of one commonly used overloaded version.

public static Assembly Load(string assemblyString)

Explanation of parameters:

  1. The assemblyString can be the name or path of the assembly to be loaded, such as the file name, full name, or file path of the assembly.

return value:

  1. Return the loaded assembly object.

Illustration:

// 加载指定路径下的程序集文件
Assembly assembly = Assembly.Load(@"C:\MyAssembly.dll");

// 加载位于GAC(全局程序集缓存)中的程序集
Assembly assembly = Assembly.Load("MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567890abcdef");

// 加载当前应用程序域中已加载的程序集
Assembly assembly = Assembly.Load("MyAssembly");

Important points to note:

  1. This method will throw an exception when the assembly cannot be loaded or does not exist. Exception handling can be used to catch and handle these exceptions.
  2. After loading the assembly with the Assembly.Load method, you can use reflection to access and manipulate information such as types and members in that assembly.
bannerAds