Click here to Skip to main content
11,932,168 members (53,747 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked

Run-Time Code Generation II: Invoke Methods using System.Reflection

, 18 Jan 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Invoke methods using System.Reflection


.NET provides mechanisms to compile source code and to use the generated assemblies within an application. Using these features, you can make applications more adaptive and more flexible.

This article shows how to invoke methods from an assembly using System.Reflection.

Necessary Using Statements

Before you start, add the following using statements to your application to ease the use of the namespaces:

using System.Reflection;
using System.Security.Permissions; 

Getting Started

At the beginning, you have to know which method from which type and from which module you want to call. Inspecting an assembly can be done very easily with System.Reflection too. But now we assume having all the knowledge about it because we generated it ourselves like in Run-Time Code Generation Part I.

So we just set the module's name, the type's name and the method's name:

string ModuleName = "TestAssembly.dll";
string TypeName = "TestClass";
string MethodName = "TestMethod";

Open an Assembly

Then you can open the assembly and set some Binding Flags, which are necessary for the instantiation of the chosen type:

Assembly myAssembly = Assembly.LoadFrom(ModuleName);

BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | 
  BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

Invoke a Method

After that, three loops are used to inspect the assembly and get the chosen module/type/method combination. Within the last loop, the type is instantiated and the method is called. The method can be called with parameters, but here we choose null for methods with no parameters. There can also be a response from the method, which we put into a general object.

Module [] myModules = myAssembly.GetModules();
foreach (Module Mo in myModules) 
     if (Mo.Name == ModuleName) 
         Type[] myTypes = Mo.GetTypes();
         foreach (Type Ty in myTypes)
            if (Ty.Name == TypeName) 
                MethodInfo[] myMethodInfo = Ty.GetMethods(flags);
                foreach(MethodInfo Mi in myMethodInfo)
                    if (Mi.Name == MethodName) 
                        Object obj = Activator.CreateInstance(Ty);
                        Object response = Mi.Invoke(obj, null);


With a few lines of code, you make your application call any method in any type from any module. In combination with code compilation, that is a powerful mechanism to enhance the adaptability of your applications.



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

André Janus
Web Developer
Germany Germany
I am currently student at the university of Karlsruhe and working on my master thesis at the IT-Management and Web Engineering Research Group within the institute of telematics.

You may also be interested in...

Comments and Discussions

QuestionWindows Form with TableLayoutPanel is changing its Controls position when is shown in mdiParent Form. How to Resolve this Issue? Pin
Nouman_gcu21-Dec-09 2:16
memberNouman_gcu21-Dec-09 2:16 
GeneralReflection , Invoke Methods using System.Reflection Pin
Nouman_gcu29-Oct-09 4:40
memberNouman_gcu29-Oct-09 4:40 
I have a Scenario in which the references of assemblies are not added to my current Visual Studio Project assembly and i need to load assembly from different paths at run time and then need to use some methods of the loaded assembly and specially in case of method overloading and overriding . I developed a sample application which is loading assembly to invoke the methods and its working fine but when I make the a method overload then its generating error. Code is given below

public class Addition
public int Add(int a, int b)
return a + b;

public int Add(float a, int b)
return (int)(a + b);

and reflection is

Assembly o = Assembly.LoadFrom("C:/dlls/TestMath.dll");
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

if (o != null)
Module[] ms = this.o.GetModules();

foreach (Module m in ms)
Type[] types = m.GetTypes();
foreach (Type tp in types)
if (tp.Name == "Addition")
MethodInfo[] mis = tp.GetMethods(flags);
foreach (MethodInfo mi in mis)
if (mi.Name == "Add")
Object obj = Activator.CreateInstance(tp);
object[] mParam = new object[] { 5.1, 10 };
int res = (int)tp.InvokeMember("Add", BindingFlags.InvokeMethod,
null, obj, mParam);

Problem is here when I call method Add with above specified parameters then its showing
following Exception "Method 'TestMath.Addition.Add' not found"

How to play with it ?
How to read the code of an assembly at run time using reflection?

Someone please help me out....

GeneralMy vote of 2 Pin
Behrooz_cs29-Jun-09 12:17
memberBehrooz_cs29-Jun-09 12:17 
QuestionWhat if... Pin
Member 22902785-Jun-08 0:07
memberMember 22902785-Jun-08 0:07 
GeneralGreat article!!. But ... Pin
SentCleef19-Apr-07 2:24
memberSentCleef19-Apr-07 2:24 
GeneralRe: Great article!!. But ... Pin
André Janus19-Apr-07 3:15
memberAndré Janus19-Apr-07 3:15 
GeneralTotally cool Pin
Mr. VB.NET3-Nov-06 14:13
memberMr. VB.NET3-Nov-06 14:13 
GeneralMore Detail Pin
qwerty666qwerty66628-Dec-05 6:09
memberqwerty666qwerty66628-Dec-05 6:09 
GeneralRe: More Detail Pin
D1113-Jun-06 9:41
memberD1113-Jun-06 9:41 
GeneralRe: More Detail Pin
mabxsi18-Aug-06 5:42
membermabxsi18-Aug-06 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 18 Jan 2006
Article Copyright 2005 by André Janus
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid