C# program to print method names and its parameters using reflection classes

Here, we are going to learn how to print method names and its parameters using reflection classes in C#?
Submitted by Nidhi, on October 28, 2020

Here, we will print method names and its parameters using predefined reflection classes Assembly, MethodInfo, and ParameterInfo.

Program:

The source code to print method names and its parameters using reflection classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print method names and 
//its parameters using reflections.

using System;
using System.Reflection;

class Sample
{
    int num1;
    int num2;

    public void SetValues(int n1, int n2)
    {
        num1 = n1;
        num2 = n2;
    }

    public void PrintValues()
    {
        Console.WriteLine("Num1 :"+ num1);
        Console.WriteLine("Num2 :"+ num2);
    }

    static void Main(string[] args)
    {
        Assembly asm;
        Type[] types;

        asm = Assembly.GetExecutingAssembly();
        types = asm.GetTypes();

        foreach (Type cls in types)
        {
            MethodInfo[] methodNames = cls.GetMethods();
            foreach (MethodInfo method in methodNames)
            {
                Console.WriteLine(method.Name);

                ParameterInfo[] param = method.GetParameters();
                foreach (ParameterInfo p in param)
                {
                    Console.WriteLine("\t"+p.Name);
                }

            }
        }
    }
}

Output:

SetValues
        n1
        n2
PrintValues
ToString
Equals
        obj
GetHashCode
GetType
Press any key to continue . . .

Explanation:

In the above program, we created two classes Sample and Program. Here, we imported the System.Reflection to use Assembly class.

The Program class contains the static method Main(), the Main() method is the entry point for the program.

Here, we created reference asm of assembly class which is initialized with object returned by the GetExecutingAssembly(), and then we get types from the current program assembly and got the name of classes that are created within the current program. After that, we got the name of methods created within the classes using the GetMethods() method of MethodInfo class and then got the name of parameters that are printed on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.