C# · CodeProject · Dotnet

Delegate Simulation

Hi Reader,

In this article i shall show/explain how we can implement the concept of delegate (multicast) with out using delegate keyword or assuming that .NET did not provide delegate feature at all.

A bit History:

I got this idea of writing an article/blog post because recently i was asked this question in an interview @Siemens. Although i answered it right, still i wanted to blog it with a simulation code.


Note: Please note that this article is not a solution for any problem in real world designs. But rather an alternate answer for delegate feature implementation. If i have missed out any thing, please revert back with a comment or email.

Basically in simple term we can say delegate itself is an object pointing (pointer concept) to another objects member viz event, methods, which is deciphered at runtime for destination object’s member execution by CLR instead of statically specifying it by developer.

In case of multicast delegate, we can queue up multiple destination type’s member calls which we need to call at some point in our code. This feature is a boon for developers actually in real world applications.

Lets me start off with the alternative way which i could think off. Basically i am using reflection concept here to simulate this feature, as of now i have simulated only for methods and not for events.

Before i start, please get the source code package from my blog here: (WordPress won’t allow me to upload zip) https://adventurouszen.files.wordpress.com/2011/09/delegate-simulation1.doc  Copy and paste the link in browser.

Note: Change the downloaded file extension to ZIP.

In order to use reflection APIs, we are suppose to use System.Reflection namespace. If you have downloaded the source code package from above link, open the file MyDelegator.cs under CustomDelegaterLib directory.

The funda here is i have 3 class variables declared viz targetType, methodName and delegatorList. Basically i am using them to hold the type object and a methodname to be called, and the list is used to hold array’s of delegator objects in case of multicasting technique.

Now, i have overloaded 2 operators + and – to add or remove the delegator objects in case of multicasting.

In .NET’s multicast delegates we can do addition or subtraction operation to add more delegates or to remove them as shown below (assumed that those delegate objects are assigned to some member of a type and not null):

Hence to simulate similar feature in our custom delegates i have overloaded basic operators.

The overloaded method is as shown below:

FillDelegatorList() method fills each passed viz. delegator1, delegator2 objects list or itself to the delegator object, so that when you call MyDelegator.Execute(), all the delegate objects listed inside it should be executed recursively.

Now, when you call Execute() method on an delegator object, if that object has a list of delegators inside it. For each delegator object in that list is pulled out via loop and method which is associated with that object is executed via reflection API. The code below shows that:

As you can see from the above code, first i am creating an instance of the specified type in an delegate object, basically the CLR shall load that type belonging to an assembly into memory (if it isn’t already).

After that, using the methodName field value, i am fetching the method details into an MethodInfo object. Note that, BindingFlags are acting like search pattern in searching methods based on their access modifiers.

If a method is found as per that name, then MethodInfo.Invoke() method calls specific method via Reflection API. Please note that, here i have assumed and written this code for parameterless void return type methods.

If you want to test with parameter methods having return types then you need to modify the above code. MethodInfo.Invoke() takes second argument as object[] for parameters to be passed. It also returns an Object to which the return type of an method is assigned.

Thanks 🙂


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s