Call a function using Reflection

Below is a code which will call a function using reflection. Note that if the function exists in an outside dll, you have to use the , separator in the className. for sure the second parameter is the name of

the function you want to invoke and the third parameter is the parameters sent to that function if exists, this parameter is of type object[].

Like i already mentioned if the function is in an external dll, send the first parameter (Class1,v.dll).


  public static void CallFunctionByReflection(string className,string functionName,object[] arguments)
  {
   Type theType = null;
   Object theObj = null;
   MethodInfo DownloadInfo = null;

   string [] type = className.Split(',');
   try
   {
    if(type.Length == 1)
    {
     theType = Type.GetType(type[0]);
    }
    else
    {
     theType = Assembly.Load(type[1]).GetType(type[0]);
    }
   
     theObj = Activator.CreateInstance(theType);   
   
                 try
                 {
                     DownloadInfo = theType.GetMethod(functionName);

                     if (arguments != null)
                     {
                         DownloadInfo.Invoke(theObj, arguments);
                     }
                     else
                     {
                         DownloadInfo.Invoke(theObj, null);
                     }
                 }
                 catch (System.Reflection.AmbiguousMatchException ame)
                 {
                     Type[] typeParams = new Type[arguments.Length];
                     for (int ind = 0; ind < typeParams.Length; ind++)
                         typeParams[ind] = typeof(System.String);

                     DownloadInfo = theType.GetMethod(functionName, typeParams);
                     DownloadInfo.Invoke(theObj, arguments);
                 }
   }
   finally
   {
    theType = null;
    theObj = null;
    DownloadInfo = null;
   }
  }

Don't forget to import the System.Reflection namespace.

Hope this helps,

Published 25 July 2007 07:26 PM by haissam

Comments

# Asad Akram said on 25 July, 2007 12:04 PM

I could not find another way to acquire your help. I am trying to make a popup calendar but it doesnot put in the value to the textbox on selection when i place the calendar in a content page of a master page. I only works when i place it in a "form". On date selection, i am writing a string command that uses window.opener.forms(0)....so i know that is causing the error but can you suggest any solution. It will be very thankfull. my email address is

Please help me with this issue..

asadlgs@hotmail.com Or

bphm@motorola.com

# Asad Akram said on 25 July, 2007 12:07 PM

Even better issue..i cant write more than 4000 chars. to a text box..into an orcale table even though i have a clob datatype for the oracle table any suggestions for guidance for that. These are very crictial issues i am facing and i am an amature at webdevelopment.

thank you for your time..please respond on my email asap.

at bphm@motorola.com

# gabgar said on 19 May, 2008 07:23 PM

Do you have updated code for this.

CallFunctionByReflection() has a first parameter of className but you have a local variable of Type called className.

>DownloadInfo = theType.GetMethod(functionName);

where is "theType" defined?

# haissam said on 20 May, 2008 01:36 AM

Thank you for your comment. I modified the code please recheck it

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating