DEV Community

bikashgosai
bikashgosai

Posted on

3 3

Reflection

Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.

The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.

Applications of Reflection
Reflection has the following applications −

It allows view attribute information at runtime.
It allows examining various types in an assembly and instantiate these types.
It allows late binding to methods and properties
It allows creating new types at runtime and then performs some tasks using those types.

// C# program to illustrate
// the use of Reflection
using System;
using System.Reflection;

namespace Reflection_Metadata {

// Define a class Student
class Student {

    // Properties definition
    public int RollNo
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    // No Argument Constructor
    public Student()
    {
        RollNo = 0;
        Name = string.Empty;
    }

    // Parameterised Constructor
    public Student(int rno, string n)
    {
        RollNo = rno;
        Name = n;
    }

    // Method to Display Student Data
    public void displayData()
    {
        Console.WriteLine("Roll Number : {0}", RollNo);
        Console.WriteLine("Name : {0}", Name);
    }
}

class Reflect {

    // Main Method
    static void Main(string[] args)
    {
        // Declare Instance of class Assembly
        // Call the GetExecutingAssembly method
        // to load the current assembly
        Assembly executing = Assembly.GetExecutingAssembly();

        // Array to store types of the assembly
        Type[] types = executing.GetTypes();
        foreach(var item in types)
        {
            // Display each type
            Console.WriteLine("Class : {0}", item.Name);

            // Array to store methods
            MethodInfo[] methods = item.GetMethods();
            foreach(var method in methods)
            {
                // Display each method
                Console.WriteLine("--> Method : {0}", method.Name);

                // Array to store parameters
                ParameterInfo[] parameters = method.GetParameters();
                foreach(var arg in parameters)
                {
                    // Display each parameter
                    Console.WriteLine("----> Parameter : {0} Type : {1}",
                                            arg.Name, arg.ParameterType);
                }
            }
        }
    }
}
}
Enter fullscreen mode Exit fullscreen mode

Output:

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay