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.