Introduction
The Assembly class in the System.Reflection namespace helps to access assembly metadata during runtime. Using this class you can examine whether or not your application assembly is loaded into memory.
The assembly class does not have a constructor, hence using the static method of assembly the instance can be created.
Code
- Assembly myassembly =Assembly.Getassembly(assemblytype)
The Assembly class provides properties to retrieve information about this class. It also provides methods to retrieve information about the modules and types contained in an assembly.
- public class MyAssembly
- {
- public void GetMetadata(Type yourType)
- {
- Assembly myassembly = Assembly.GetAssembly(yourType);
- Type[] TypesInAssembly = myassembly.GetExportedTypes();
- AssemblyName[] referencedAssemblies= myassembly.GetReferencedAssemblies();
-
- Console.WriteLine("Type name: " + yourType.Name+" is contained in assembly");
- Console.WriteLine(myassembly.FullName);
- Console.WriteLine("This assembly references "+referencedAssemblies.GetLength(0).ToString()+ "other assemblies");
- Console.WriteLine("There are "+TypesInAssembly.GetLength(0).ToString()+" public types in this assembly.");
- Console.WriteLine();
- Console.ReadLine();
- Console.WriteLine("Referenced Assemblies:");
- Console.WriteLine();
-
- foreach (AssemblyName assemblyName in referencedAssemblies)
- {
- Console.WriteLine(assemblyName.FullName);
- Console.WriteLine();
- }
- Console.ReadLine();
- Console.WriteLine("Public types in assembly");
- foreach (Type tp in TypesInAssembly)
- {
- Console.WriteLine(tp.FullName);
- Console.WriteLine();
- }
- }
- }
The preceding code displays the name of the assembly, the public types used in the assembly and the other assemblies that this assembly references. As I said above, the Assembly class does not have a constructor, hence in the preceding code the GetAssembly static function is used to create an instance of the assembly class.
The static function named GetExportedTypes helps to get a list of public types (types that are visible outsite the assembly), what this assembly uses and the function GerReferenceAssemblies lists the assemblies that your assemblies are referencing.
Summary
I hope you have learned how to get assembly metadata.