Saturday, May 13, 2023

What is reflection in c#?|Reflection in c#| c# reflection| reflection

 What is reflection in c#?


Reflection is a feature in C# that allows you to obtain information about types, methods, properties, and other members of an object at runtime. Reflection enables you to examine and manipulate objects, types, and assemblies at runtime, providing a great deal of flexibility and extensibility.


Reflection is typically used in scenarios where you need to load and manipulate types at runtime, such as creating instances of objects or invoking methods dynamically. Reflection provides a powerful set of APIs that enable you to inspect and interact with types and objects at runtime.


Here are some examples of what you can do with reflection:


1. Load assemblies at runtime: You can use reflection to load assemblies and types dynamically at runtime.


2. Create instances of objects dynamically: You can use reflection to create instances of objects dynamically, without knowing the type at compile time.


3. Invoke methods dynamically: You can use reflection to invoke methods on objects dynamically, without knowing the method signature at compile time.


4. Inspect and manipulate properties and fields: You can use reflection to inspect and manipulate the properties and fields of objects dynamically, without knowing their names at compile time.


Here's an example that demonstrates some of the basic capabilities of reflection:



using System;

using System.Reflection;


class Program

{

    static void Main(string[] args)

    {

        // Load the assembly containing the System.String class

        Assembly assembly = Assembly.Load("mscorlib");


        // Get the System.String type

        Type stringType = assembly.GetType("System.String");


        // Create a new instance of the System.String class

        object strObj = Activator.CreateInstance(stringType);


        // Invoke the System.String.ToUpper method dynamically

        MethodInfo toUpperMethod = stringType.GetMethod("ToUpper");

        string result = (string)toUpperMethod.Invoke(strObj, null);


        Console.WriteLine(result); // outputs an empty string in upper case

    }

}



In this example, we use reflection to load the mscorlib assembly, get the System.String type, create a new instance of the System.String class, and invoke the ToUpper method dynamically. Note that we use reflection to obtain the MethodInfo object representing the ToUpper method, and we invoke the method dynamically using the Invoke method.


In summary, reflection is a powerful feature in C# that allows you to examine and manipulate objects, types, and assemblies at runtime. Reflection enables you to create more flexible and extensible code, and is commonly used in dynamic programming scenarios.

No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...