C# Program to Print Method Names and Its Parameters Using Reflection Classes

You are currently viewing C# Program to Print Method Names and Its Parameters Using Reflection Classes

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

//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 . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply