Code Reflection in C#
This article will
introduce you to Code Reflection in C#. While the focus is on C# language here,
the concepts can be applied to any technology based on Visual C#, such as
ASP.NET, Windows Phone or Windows Store Apps.
This article is also
available of the Microsoft TechNet Wiki.
This
article was selected The Microsoft TechNet Guru Awards!
(November 2014)
introduce you to Code Reflection in C#. While the focus is on C# language here,
the concepts can be applied to any technology based on Visual C#, such as
ASP.NET, Windows Phone or Windows Store Apps.
available of the Microsoft TechNet Wiki.
article was selected The Microsoft TechNet Guru Awards!
(November 2014)
Introduction
Reflection is the ability of a managed code to read its own metadata.
You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
If you are using attributes in your code, reflection enables you to access them.
Uses of Code Reflection
Below are some of the uses of code reflection:
1. Check the type of an object at run-time using MetaData
2. Examining and instantiating types in an assembly.
3. Dynamically create type instances and dynamically invoke the methods, fields, properties in created instances.
4. For performing late binding and accessing methods on types created at run time.
Classes used in this article
This article makes use the the following classes below
Getting Type data of an Object
To obtain the Type of an object dynamically at run time, GetType method can be used.
Creating an Instance of a type
a. Creates an instance of the specified type using that type’s default constructor.
b. Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.
Example 1: Creating an Object Dynamically.
The example below demonstrates how to create a Pizza object dynamically, retrieve its properties and assign their values.
Step 1. Creating the instance of the Type Pizza.
Step 2. Retrieving its properties.
The GetProperties Method, searches for the properties of the current Type, using the specified binding constraints.
Step 3: Setting the value of each property.
Getting the value of a Property from an Object
Step 1: Get the type of the object.
Step 2: Get the required property.
Step 3: Get the value of the property.
Invoking methods from an Object
Step 1: Get the type of the object.
Step 2: Get the method needed.
In this example, there 2 methods with the same name.
To get the method having no parameter, an empty array of type Type should be passed.
To get the method having a string parameter, pass the parameter in an array like below
Step 3: Invoking the method.
To invoke the method with no parameter, null should be passed to the invoke method.
To invoke methods requiring parameters, the parameters can be passed in an array like below
Example 2: A generic method with different behavior based on Object Type
The example below shows how the output can be different on whether a Pizza or a Burger is passed to the PrintReceipt Method.