Reflection

Reflection is a way to query metadata at runtime from the application program. Reflection supplies type information from the assemblies loaded into memory that you can use to create an instance of the class and also access properties and methods of the class.

For example, your application code executes a query and returns a dataset object, but your frontend accepts a custom class or model, and the model is defined during runtime. Based on the request received, reflection can be used to create the required model/class at runtime, access its properties or fields, and set their value by traversing through the resulting dataset.

Additionally, in previous sections, we learned how we can create custom attributes. So, in a scenario where you create an attribute to restrict numbers in a specific property, you can then use reflection to read the attribute, get the preceding property, implement application logic to restrict numbers, or display a message to users.

We can also use reflection to create a type at runtime and access its methods and properties. Reflection works with System.Types to query information about assemblies that are currently loaded into memory and are being executed.

The Common Language Runtime (CLR) manages application domains with boundaries around objects that are of the same scope.  This process includes loading assemblies into these domains and controlling them as required.

In the .NET world, assemblies contain modules, modules contain types, and types contain members. An assembly class is used to load assemblies. Modules are used to identify information about classes in the assembly as well as global and non-global methods.

There are many methods available in the Reflection class, such as MethodInfo, PropertyInfo, Type, CustomAttribute, and many more. These methods help developers to retrieve information at runtime. In the previous example, we used the GetCustomAttribute method to retrieve attribute information and displayed it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.163.250