Getting Assembly Metadata at Runtime


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.
div>The assembly class does not have a constructor, hence using the static method of assembly the instance can be created.
  1. 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.  
  1. public class MyAssembly  
  2.     {  
  3.         public void GetMetadata(Type yourType)  
  4.         {  
  5.             Assembly myassembly = Assembly.GetAssembly(yourType);  
  6.             Type[] TypesInAssembly = myassembly.GetExportedTypes();  
  7.              AssemblyName[] referencedAssemblies=   myassembly.GetReferencedAssemblies();  
  9.              Console.WriteLine("Type name: " + yourType.Name+" is contained in assembly");  
  10.              Console.WriteLine(myassembly.FullName);  
  11.              Console.WriteLine("This assembly references "+referencedAssemblies.GetLength(0).ToString()+ "other assemblies");  
  12.              Console.WriteLine("There are "+TypesInAssembly.GetLength(0).ToString()+" public types in this assembly.");  
  13.              Console.WriteLine();  
  14.              Console.ReadLine();  
  15.              Console.WriteLine("Referenced Assemblies:");  
  16.              Console.WriteLine();  
  18.              foreach (AssemblyName assemblyName in referencedAssemblies)  
  19.              {  
  20.                  Console.WriteLine(assemblyName.FullName);  
  21.                  Console.WriteLine();  
  22.              }  
  23.              Console.ReadLine();  
  24.              Console.WriteLine("Public types in assembly");  
  25.              foreach (Type tp in TypesInAssembly)  
  26.              {  
  27.                  Console.WriteLine(tp.FullName);  
  28.                  Console.WriteLine();  
  29.              }  
  30.         }  
  31.     }  
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 outside the assembly), what this assembly uses and the function GerReferenceAssemblies lists the assemblies that your assemblies are referencing.


I hope you have learned how to get assembly metadata.