Click here to Skip to main content
13,353,462 members (39,193 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 25 Jan 2005

Conditional Invoking of Methods Using Delegates and Reflection

, 25 Jan 2005
Rate this:
Please Sign up or sign in to vote.
This document describes how to implement conditional or cyclic invoking of methods using such features of C# and the .NET Framework as delegates and Reflection. The document contains a number of C# code examples with comments.


In many cases, developers have a demand to call methods testing some conditions before, like, if or switch statements. In most cases, developers call different methods on different conditions. And sometimes, developers need to call different methods either in loops or consequently. All cases mentioned above are covered by this article. Here, you can find several C# code examples that demonstrate how to solve such tasks in an efficient manner. Hope this article will be helpful.

Conditional Invoking with Reflection

Let’s say we need to call different methods based on the value of a string. We can easily do this using an if statement:

if (key == "Key1")
else if (key == "Key2")

If we have to compare three or more values, we can use a switch statement:

    case "key1":
    case "key2":
    case "key3":

But what if we have to test out ten or twenty or however many values and call the different methods for different values? We can still use the examples above for sure, but we also can use the following. Let’s define a naming convention for methods that we are going to call in such a manner that the method’s name will contain a compared value or any part of it, just like the following example:


and so on. So we can use the following code to call these methods:

Type t = typeof(MyClass);
string key = "Key1";
t.InvokeMember( key + "Method", 
    BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod | BindingFlags.Instance,
    null, this, null);

Here, MyClass is the name of the class that actually contains all those methods, and the InvokeMember is a standard .NET Framework method to invoke any method in run-time based on Reflection information. The proposed approach is very easy to modify to use methods with parameters too. You can find all necessary information about the InvokeMember in the Visual Studio .NET’s help. You can also not only use string values as a variable part of a method’s name but any type that you can represent as a string using the standard ToString() method as well.

This sample code could seem slower as long as it uses reflection. But it saves a lot of the developer’s time and provides a compact, flexible, and easily manageable code, not saying that it also may prevent unexpected mistakes usual to large if or switch statements.

Conditional Invoking with Delegates

Let’s solve the same task using delegates. First of all, let’s define a delegate type for our methods. Guess it is not necessary to mention that all methods should expose the same signature.

delegate void MethodToInvoke();

Then, let’s create a so called Table of Calling using a standard Hashtable for instance.

Hashtable methods = new Hashtable();
    methods.Add("Key1", new MethodToInvoke(Key1Method));
    methods.Add("Key2", new MethodToInvoke(Key2Method));
    methods.Add("Key3", new MethodToInvoke(Key3Method));

And since we successfully made all preparations, finally we can call any of our "key" methods very simply, based on the value of the key variable:

string key = "Key1";

Cycling Invoking

Using the pattern described above, we can easily implement a cycling methods invoking, that is a sequential calling of methods in a loop. Let’s see the following code:

for (int i=0; i<methods.Count; i++)

That approach may be useful, for example, when you develop using the Pipeline Design Pattern and require calling several methods one by one. You may say that it’s very similar to what multicast delegates do when you initialize them with the += operator. That’s true. But let’s figure out a difference. Multicast delegates must have the void return type. So you can’t use it if you want a delegate to return something. But the proposed approach allows you to do this easily:

delegate bool MethodToInvoke();
for (int i=0; i<methods.Count; i++)
    if ( true != ((MethodToInvoke)methods[i])() )


In this article, I have shortly described several approaches of method calling using such features of the .NET Framework and C# language as Reflection and delegates. Using these features lets developers increase the code flexibility, readability and manageability, and sometimes allows to prevent some mistakes connected with if and switch statements. I could bring much more examples of code using the concepts described. But the purpose of this article is just to illustrate a common idea of using delegates and reflection in cases of conditional method invoking. I guess that the goal is pretty much covered by this article.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alexander Turlov
Canada Canada
Alexander Turlov is a professional software development consultant that has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, Basic, C, C++ and C#. He's been working for different industries including but not limited to science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of professional interests is cloud powered rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, Azure and Visual Studio Team Services as his primary tools. He holds a M.Sc. degree in physics and various industry certifications including MCSD.NET, Azure and Scrum.

View my profile on LinkedIn

View my blog

You may also be interested in...


Comments and Discussions

BugYour code is not working for me Pin
vikashevs13-Jan-14 21:29
membervikashevs13-Jan-14 21:29 
GeneralRe: Your code is not working for me Pin
vikashevs13-Jan-14 21:39
membervikashevs13-Jan-14 21:39 
QuestionWhat about the "Visitor" pattern? Pin
tkrafael_net30-Jun-08 2:52
membertkrafael_net30-Jun-08 2:52 
GeneralThank you colleagues! Pin
Alexander Turlov8-Feb-05 7:12
memberAlexander Turlov8-Feb-05 7:12 
GeneralGood Pin
Serge Lobko-Lobanovsky26-Jan-05 0:59
memberSerge Lobko-Lobanovsky26-Jan-05 0:59 
GeneralRe: Good Pin
Alexander Turlov26-Jan-05 6:05
memberAlexander Turlov26-Jan-05 6:05 
GeneralRe: Good Pin
Serge Lobko-Lobanovsky26-Jan-05 6:18
memberSerge Lobko-Lobanovsky26-Jan-05 6:18 
Yep, ja tozhe russkij Smile | :) I didn't know you can could call C++ methods indirectly (like it is done using Reflection). Is there such a generic way in C++?

my blog on Easy Projects .NET and object-relational mapping
GeneralRe: Good Pin
Alexander Turlov26-Jan-05 7:19
memberAlexander Turlov26-Jan-05 7:19 
GeneralRe: Good Pin
vt89qn21-May-12 2:05
membervt89qn21-May-12 2:05 
GeneralRe: Good Pin
vt89qn21-May-12 2:06
membervt89qn21-May-12 2:06 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 25 Jan 2005
Article Copyright 2005 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid