Thursday, October 6, 2011

C#.net - Dynamic Invoke Method in C#.net Using Reflection

In this article i will show how to dynamic invoke static or non static method using Reflection.


Step 1
Create a Console Application an give the solution name as SolConDynamicInvokeMethod.


Step 2
Create a class and write four methods such as Static method,Non static Method,Parameterized Method and Generic Method ,it is look like this 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SolConDynamicInvokeMethod
{
    public class DynamicClass
    {
        /// 
        /// Static Function
        /// 
        /// String
        public static String Method1()
        {
            try
            {
                return "C#.net - Static Method";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// 
        /// Non Static Method
        /// 
        /// 
        public String Method2()
        {
            try
            {
                return "Vb.net - Non Static Method";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// 
        ///  Parameterized Method
        /// 
        /// 
        /// 

        public void Method3(String FirstLanguageName, String SecondLanguageName)
        {
            try
            {
                System.Console.WriteLine(FirstLanguageName);
                System.Console.WriteLine(SecondLanguageName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// 
        ///  Generic Method
        /// 
        /// 
        /// 
        /// 
       public String Method4<t>(T TObj)
        {
            try
            {
                return TObj as string;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

Step 3
Invoke Method,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SolConDynamicInvokeMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get Type of the Given Class
            Type TypeObj = typeof(DynamicClass);

            // Create a Instance of the specified type
            Object MyObj = Activator.CreateInstance(TypeObj);


            // Invoke Static Method
            String StaticMethod = (String)TypeObj.InvokeMember("Method1", BindingFlags.InvokeMethod | BindingFlags.Default, null, null, new Object[] { });
            System.Console.WriteLine(StaticMethod);


            // Invoke Non Static Method
            String NonStaticMethod = (String)TypeObj.InvokeMember("Method2", BindingFlags.InvokeMethod | BindingFlags.Default, null, MyObj, new Object[] { });
            System.Console.WriteLine(NonStaticMethod);


            // Invoke Parameterized Method
            TypeObj.InvokeMember("Method3", BindingFlags.InvokeMethod | BindingFlags.Default, null, MyObj, new Object[] { "C#.net", "Vb.net" }); 


            // InVoke Generic Method
            MethodInfo MethodObj = typeof(DynamicClass).GetMethod("Method4");
            MethodInfo GenericMethodObj = MethodObj.MakeGenericMethod(typeof(String));
            String GenericMethod=(String) GenericMethodObj.Invoke(MyObj,new Object[]{"I Love .Net Technology"});
            System.Console.WriteLine(GenericMethod);   

           
        }
    }

    
}


Run the Project.


Download
Download Source Code

1 comment:

  1. Thanks for this! One thing that I would not do is

    catch (Exception ex)
    {
    throw new Exception(ex.Message);
    }

    It does nothing and only clutters your great examples.

    ReplyDelete