Reflection - Invoking a Member Method

Here we will see how to invoke a member method by using reflection.

1. Create a class library with the name MathLib;

2. Add the following code to it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MathLib
{
    public class Maths
    {
        public void Add(int a, int b)
        {
            int c = a + b;
            Console.WriteLine("Result From Calculator after Add:" + c);
        }
        private void Sub(int a, int b)
        {
            int c = a - b;
            Console.WriteLine("Result From Calculator after Add:" + c);
        }
    }
}

3. Compile the class library; the result will be a MathLib.dll file in "projectfolder\bin\release" folder

4. Now we want to invoke the library's methods from another application without adding the reference before compilation, instead accessing an assembly at run time and even without knowing it's name or any other details, call a method from that library - this sounds really dynamic.

To do so create a console application and use the following code. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;  //for reflection
using System.Globalization; //for  culture-optional

namespace SimpleInvokeDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //loading assembly at run time
            Assembly assembly = Assembly.Load(@"c:\MathLib.dll");
            //Getting list of types in Mathlib.dll
            Type[] t = assembly.GetTypes();
            //getting all methods from first type.here Maths class
            MethodInfo[] ms = t[0].GetMethods(); //only one class maths
            string strTypename = t[0].Namespace + "." + t[0].Name; //namespace.classname
            //object creation for method invocation
            object obj = assembly.CreateInstance(strTypename);
            foreach (MethodInfo m in ms)
            {
                //gettting the parameters
                ParameterInfo[] pinfo = m.GetParameters();
                if (pinfo.Length == 2)
                // if (m.Name.Equals("Add") || m.Name.Equals("Sub"))
                {
                    object[] pobjs = new object[2];
                    if (pinfo[0].ParameterType == typeof(int))
                        pobjs[0] = 50;
                    if (pinfo[1].ParameterType == typeof(int))
                        pobjs[1] = 25;
                    //calling the methods with to int aggs in th mathslib.dll with minimum options
                    //object of the class to which th method is belonging to ,and array of parameters
                    m.Invoke(obj, pobjs);
                    CultureInfo cul = new CultureInfo("hi-IN");
                    //calling the methods with to int aggs in th mathslib.dll with binding flags
                    m.Invoke(obj, BindingFlags.NonPublic, null, pobjs, cul);
                }
            }
            Console.ReadLine();
        }
    }
}

Note culture and binding flags can be optional.

Hope you are able to work with this example.