Calling a method using Reflection in C#

Posted by Raja under C# category on | Views : 8934
Below is the code snippet to call a method using reflection.

// Name of the .dll file
Assembly asm = Assembly.Load("DNFBusinessLayer");

// Name of the class where the method resides
Type tsm = asm.GetType("DNFBusinessLayer.MyManager");

// "Load" is the method name, if this method expect some parameter pass them as the last parameter as the array of objects
Object obj = tsm.InvokeMember("Load", BindingFlags.InvokeMethod, null, tsm, new object[0]);

// Now suppose you have to bind the returned data in GridView, specify as DataSource (here I am assuming that it Load method will return collections
GridView1.DataSource = obj;

Hope this will help.


Comments or Responses

Posted by: Poster on: 12/22/2009 Level:Starter | Status: [Member]
This is good one Raja. Thanks for posting.

Login to post response