Last Update: Dec 31, 2022

Check out my YouTube Channel!

An assembly is a (compiled) code library used in .NET applications. An assembly contains one or more types and resources. It’s usually in the form of a .DLL file.

There are several ways to open an assembly in C#:

1. Using the Assembly.Load method:

Assembly assembly = Assembly.Load("YourAssemblyName");

This method loads an assembly with the specified name. The assembly name can be a short name, such as “MyAssembly”, or a fully qualified name, such as “MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8675309abcdefg”.

2. Using the Assembly.LoadFrom method:

Assembly assembly = Assembly.LoadFrom("path\\to\\yourassembly.dll");

This method loads an assembly from the specified file path.

3. Using the Assembly.LoadFile method:

Assembly assembly = Assembly.LoadFile("path\\to\\yourassembly.dll");

This method loads an assembly from the specified file path, and it has the same behavior as Assembly.LoadFrom, except that it does not load dependencies from the global assembly cache.

Using the GetTypes to Access Your Types

Once you have loaded an assembly, you can use the GetTypes method to get a list of all the types contained in the assembly, and the CreateInstance method to create an instance of a specific type.

For example:

Type[] types = assembly.GetTypes();
foreach (Type ourType in types)
{
    Console.WriteLine(ourType.Name);
}
object obj = assembly.CreateInstance("TypeName");

And that’s all there is to it!

Questions, comments? Let me know!



Can you beat my SkillIQ Score in C#??

C# Skill IQ
I think you can! Take the C# test here to see where you stand!





Published: Dec 11, 2022 by Jeremy Morgan. Contact me before republishing this content.