ParameterInfo in Assemblies using C#

In this article we will discuss about how to get the information of the parameter of a method in Assemblies using C#.
  • 2941

If our assemblies contains the method which contains any parameters and we want to get the parameter information of them then we will use the ParameterInfo class of the System.Reflection namespace and also we will use the GetParameters method to get the parameter of the method. Late bindings can be achieved by using reflection. For example, in some applications, we don't know which assembly to load during compile time, so we ask the user to enter the assembly name and type during run time and the application can load assembly.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

 

namespace reflection

{

    class Program

    {

        static void Main(string[] args)

        {

            Assembly asm = Assembly.LoadFrom(@"D:\meghawebsite\reflassm\reflassm\bin\Debug\reflassm.dll");

            Type[] type = asm.GetTypes();

            foreach (Type t in type)

            {

                if (t.Name == "mycls")

                {

                    MethodInfo[] minfo = t.GetMethods();

                    foreach (MethodInfo m in minfo)

                    {

                        if (m.Name == "show")

                        {

                            ParameterInfo[] pinfo = m.GetParameters();

                            foreach (ParameterInfo p in pinfo)

                            {

                                Console.WriteLine(p.Name);

                                Console.WriteLine(p.ParameterType.ToString());

 

                            }

                        }

                    }

                }

            }

            Console.ReadLine();

        }

    }

}

Output:

 image4.jpg

In the above output it is clear that i is a parameter of show method and it is of integer type.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.