Retrieve method information using reflection in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2973 red flag

How to get information of various method from assembly using reflection.

Retrieve method information using reflection in C#

Refection is very important to retrieve own assembly information. Sometimes it is needed to get information of various classes and function of current assembly. In this situation we can use reflection technique to fetch all those information. Here we will see how to get information of function from assembly.

Read function name from class using reflection.

Here we will try to read all function names from a certain class using reflection technique. In below example two functions are there within Test class. And from main function we will fetch those function name.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
namespace Test1
{
    class Test
    {
        public void A()
        {
        }
        public void B()
        {
        }
    }
 
   class Program
    {
       static void Main(string[] args)
       {
            Type obj = typeof(Test);
            MethodInfo[] t = obj.GetMethods();
            foreach (MethodInfo m in t)
            {
                Console.WriteLine(m.Name);
            }
            Console.ReadLine();
       }
    }
}

Here is sample output.


Read parameter name and type

In this example we will retrieve parameter information and their types using reflection. Have a look on below code.  

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
namespace Test1
{
    class Test
    {
        public void A(int a)
        {
        }
        public void B(String b)
        {
        }
    }
 
   class Program
    {
       static void Main(string[] args)
       {
            Type obj = typeof(Test);
            MethodInfo[] t = obj.GetMethods();
 
            foreach (MethodInfo m in t)
            {
                ParameterInfo[] p = m.GetParameters();
                foreach (ParameterInfo p1 in p)
                {
                    Console.WriteLine("Name "+ p1.Name + "   "+ "Paramiter type " +  p1.ParameterType);
                }
            }
            Console.ReadLine();
       }
    }
}


Check input or output parameter

If we want, we can check whether the parameters are input or output parameter using IsOut and IsIn property. Try to understand below example.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
namespace Test1
{
    class Test
    {
        public void A(int a)
        {
        }
        public void B(String b, out string c)
        {
            c = "sourav";
        }
    }
 
   class Program
    {
       static void Main(string[] args)
       {
            Type obj = typeof(Test);
            MethodInfo[] t = obj.GetMethods();
 
            foreach (MethodInfo m in t)
            {
                ParameterInfo[] p = m.GetParameters();
                foreach (ParameterInfo p1 in p)
                {
                    Console.WriteLine("Member : " +p1.Member +"Input/Output : "  + p1.IsOut.ToString());
                }
            }
            Console.ReadLine();
       }
    }
}

 

 


Conclusion:-

This is the technique to retrieve information from own assembly. 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)