Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. Some of the commonly used classes of System.Reflection are:
|Assembly||describes an assembly which is a reusable, versionable, and self-describing building block of a common language runtime application|
|AssemblyName||Identifies an assembly ith a unique name|
|ConstructorInfo||Describes a class constructor and gives access to the metadata|
|MethodInfo||Describes the class method and gives access to its metadata|
|ParameterInfo||Describes the parameters of a method and gives access to its metadata|
|EventInfo||Describes the event info and gives accessto its metadata|
|PropertyInfo||Discovers the attributes of a property and provides access to property metadata|
|MemberInfo||Obtains information about the attributes of a member and provides access to member metadata|
Note: There are numerous other classes, the above table gives info about only the commonly used.
Let us now look at an example to depict how reflection works in C#.
Example 1: In the code given below, we load the type t as a string using the typeof method. Then we apply reflection on t to find any information about string class, like its name, fullname, namespace, and basetype.
Name : String Full Name : System.String Namespace : System Base Type : System.Object
Example 2: In this code, we use reflection to show all the metadata related to the program which includes classes, methods of these classes and the parameters associated with these parameters.
Class : Student --> Method : get_RollNo --> Method : set_RollNo ----> Parameter : value Type : System.Int32 --> Method : get_Name --> Method : set_Name ----> Parameter : value Type : System.String --> Method : displayData --> Method : ToString --> Method : Equals ----> Parameter : obj Type : System.Object --> Method : GetHashCode --> Method : GetType Class : Program --> Method : ToString --> Method : Equals ----> Parameter : obj Type : System.Object --> Method : GetHashCode --> Method : GetType
- Late Binding using Reflection in C#
- Hello World Program : First program while learning Programming
- Range and Indices in C# 8.0
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
- Range Constructor in C#
- Finding the Start Index of the Specified Range in C#
- Finding the End Index of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- 5 Best Programming Languages For Newbies
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.