System.Reflection Namespace
Contains types that retrieve information about assemblies, modules, members, parameters, and other entities in managed code by examining their metadata. These types also can be used to manipulate instances of loaded types, for example, to hook up events or to invoke methods. To dynamically create types, use the System.Reflection.Emit namespace., see the official webpage.
Assembly class use assembly type provides various properties and methods for getting any kind of information about assembly, getting loaded assemblies, and loading assemblies.
See this list of some useful properties and description:
- Codebase – returns the location of the assembly;
- EntryPoint – returns the method information of the entry methods (Main method). Console applications return a reference to the Main method and dynamic link libraries (DLL) return NULL;
- FullName – returns the full name of the assembly including version number, Culture, and PublicKeyToken;
- GlobalAssemblyCache – this is bool property returns true if assembly loaded from GAC else returns false;
- CustomAttributes – returns all the custom attributes of the assembly;
- DefinedTypes – returns all the types in the assembly;
- Modules – return all the modules in the assembly;
- Codebase – returns the location of the assembly;
- EntryPoint – returns the method information of the entry methods – Main method. The console applications return reference to the Main method and dynamic link libraries (DLL) returns NULL;
- FullName – returns the full name of the assembly including version number, Culture, and PublicKeyToken;
- GlobalAssemblyCache – bool property returns true if assembly loaded from GAC else returns false;
- CustomAttributes – returns all the custom attributes of the assembly;
- DefinedTypes – returns all the types in the assembly;
- Modules – return all the modules in the assembly.
There are two types of assemblies:
- Executable files (EXE)
- Libraries (DLL)
This is a simple example:
This is the source code in C# :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.Collections.Generic; using System.Reflection; using System.Text; public class Program { public static void Main() { Type getType = Type.GetType("System.Reflection.Assembly" ); Console.WriteLine("\n Type = {0}\n", getType ); MemberInfo[] memberInfoArray =getType.GetMembers(); foreach ( MemberInfo memberInfo in memberInfoArray ) { //Console.WriteLine($"BuildConfig={config}"); Console.WriteLine( "MemberType={0} | Name={1}",memberInfo.MemberType, memberInfo.Name ); } } } |
You can use this form of usage with var:
1 | var assembly |
- var – requires less typing, is shorter, and easier to read;
- var – requires code changes if the return type of a method call changes and to change the method declaration, not every place it’s used;
- var – encourages you to use a descriptive name for the variable.
The assembly returns a type in an Assembly.
Here is another example with this feature:
GetCustomAttributesData – returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.
This is the source code in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | using System; using System.Reflection; using System.Linq; public class Assemblies_001 { public static void Main () { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { var name = assembly.GetName(); Console.WriteLine("=============="); Console.WriteLine($"Name={name.Name} Version={name.Version} Location={assembly.Location}"); Console.WriteLine("-------------"); // need to use System.Reflection var config = assembly.GetCustomAttribute<AssemblyConfigurationAttribute>()?. Configuration; Console.WriteLine($"BuildConfig={config}"); Console.WriteLine("~~~~~~~~~~~~~~"); foreach (var attribute in assembly.GetCustomAttributesData()) { Console.WriteLine(">> Attribute is: " + attribute); } Console.WriteLine("+++++++++++++++"); // need to use using System.Linq - Enumerable var dataList = assembly.GetCustomAttributes<AssemblyMetadataAttribute>() ?? Enumerable.Empty< AssemblyMetadataAttribute>(); foreach (var data in dataList) { Console.WriteLine($"{data.Key}={data.Value}"); } Console.WriteLine("==============="); } } } |