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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay