SIGN UP MEMBER LOGIN:    
ARTICLE

Reflection - Invoking a Member Method

Posted by PRASANTH T P Articles | C# Assemblies June 09, 2010
In this article we will see how to invoke a member method by using reflection.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor