Reflection is the process of examining and manipulating the structure and behaviour of program at runtime.With Reflection anybody can determines types, methods, properties, Constructor and other members of your program dynamically, without necessarily knowing them at compile time. Reflection provides a powerful way to interact with types and objects in a flexible manner.you can use reflection to get information about events defined within a type, such as their names, event handlers, and other metadata.
Reflection can be used to get information about types at runtime, such as their properties, methods, fields, and attributes.
You can use reflection to create instances of types (properties, methods, fields, and attributes) at runtime, even if you only know the type (properties, methods, fields, and attributes) name at runtime.
Reflection can be used for methods and properties invokation at runtime, This is specially useful when you don't know the member names at compile time.
You can use reflection to get and chang the fields of objects at runtime.
Reflection can be used to inspect the attributes applied to types, fields, methods, properties, constructor etc and can apply a necessary action based on them.
// Get assembly dynamically
// Get the type dynamically
// Create an instance of the type dynamically
// Access a property dynamically
// Invoke a method dynamically
// Access the property value
// Get assembly dynamically
// Get the type dynamically
// Create an instance of the type dynamically
// Access a property dynamically
// Invoke a method dynamically
// Access the property value
Here in below code we have defined event. We have created an instance of the class and getting the type of the class, getting the event defined in the class and displaying information about each event, displaying information about the event handler delegate type.
// Raise the event
// Create an instance of the class
// Get the type of the class
// Get the event defined in the class
// Display information about each event
// Display information about the event handler delegate type
// Raise the event
// Create an instance of the class
// Get the type of the class
// Get the event defined in the class
// Display information about each event
// Display information about the event handler delegate type
Here in below code we have defined enum. In main method we are getting assembly dynamically and displaying the name of the enum, we are also getting enum values via reflection code. We are also displaying enum value name and underlying string value and getting the type dynamically. We have also created an instance of the type dynamically and accessing a enum dynamically.
// Get assembly dynamically
// Display the name of the enum
// Get enum values
// Display enum value name and underlying string value
// Get the type dynamically
// Create an instance of the type dynamically
// Access a enum dynamically
// Get assembly dynamically
// Display the name of the enum
// Get enum values
// Display enum value name and underlying string value
// Get the type dynamically
// Create an instance of the type dynamically
// Access a enum dynamically