Take the 2-minute tour ×

I want to be able to do the equivalent to the following at runtime:

var action = new Action<ANYTHING AT RUNTIME>(obj => Console.WriteLine("Called = " + obj));

I know I need to get the correct type for the Action, but not sure how to get the final bit using Delegate.Create. Type represent T in the Action definition.

var actionType = typeof(Action<>).MakeGenericType(Type);
var constructor = actionType.GetConstructors()[0];
var @delegate = Delegate.CreateDelegate(actionType, <WHAT GOES HERE>);

the point people seem to be missing is I'm trying to create an instance of Action where T can not be specified statically because it is being used from a class derived from Attribute - this means T could be anything and it can not be defines as a generic definition


share|improve this question
Which part of action = new Action<int>(obj => Console.WriteLine("Called = " + obj)); do you want to generate dynamically? –  Mark Aug 26 '12 at 16:26
I want to create\initialise the parameter 'action' –  AwkwardCoder Aug 26 '12 at 16:42
I'm confused. There is no parameter called action. What do you want to achieve? –  Mark Aug 26 '12 at 16:52
Sorry meant @delegate –  AwkwardCoder Aug 26 '12 at 21:12
As you can see in the answers, people does not understand the point. Could you may explain the context and what you want to achieve? –  devundef Aug 28 '12 at 17:31

4 Answers 4

If you know what the operation you need to perform is and how to perform it regardless of type (as in your example) why not just make a generic method that performs the operation and create your delegate that way?

class Program
    public static void Perform<T>(T value)
        Console.WriteLine("Called = " + value);

    public static Delegate CreateAction(Type type)
        var methodInfo = typeof (Program).GetMethod("Perform").MakeGenericMethod(type);
        var actionT = typeof (Action<>).MakeGenericType(type);
        return Delegate.CreateDelegate(actionT, methodInfo);

    static void Main(string[] args)
        CreateAction(typeof (int)).DynamicInvoke(5);
share|improve this answer

You can use following code, it works if type can be casted to an object:

Action<object> func = o => Console.WriteLine("Called = " + o.GetType().Name);
var actionType = typeof(Action<>).MakeGenericType(type);
var constructor = actionType.GetConstructors()[0];
var @delegate = Delegate.CreateDelegate(actionType, func.Method);

However if type is enum or other value type it won't work.

share|improve this answer

The short answer is to create a delegate MyActionDelegate and then use:

delegate void MyActionDelegate(T arg);
Delegate @delegate = new MyActionDelegate((a) => Console.WriteLine(a));

Here's a working example using a generic class:

public class MyClass<T>
    public delegate void ActionDelegate(T arg);

    public void RunGenericAction(T arg)
        var actionType = typeof(Action<>).MakeGenericType(typeof(T));
        var constructor = actionType.GetConstructors()[0];
        Delegate @delegate = new ActionDelegate((a) => { Console.WriteLine(arg); });
        var inst = (Action<T>)constructor.Invoke(new object[] { 

Use it like this, which outputs 123 to the console:

var c = new MyClass<int>();

You'll notice I'm passing two parameters to Constructor.Invoke; that's because it turns out that a delegate argument actually compiles as two arguments: the target object of the function, and a pointer to the function. I can't take credit for the fancy footwork there; "borrowed" info from this excellent answer on how to pass a delegate argument using reflection.

share|improve this answer

Use the following code to create a delegate, which is late bound in the type parameter. See also How to: Examine and Instantiate Generic Types with Reflection.

abstract class ActionHelper {

    protected abstract Delegate CreateActionImpl();

    // A subclass with a static type parameter
    private class ActionHelper<T> : ActionHelper {
        protected override Delegate CreateActionImpl() {
            // create an Action<T> and downcast
            return new Action<T>(obj => Console.WriteLine("Called = " + (object)obj));

    public static Delegate CreateAction(Type type) {
        // create the type-specific type of the helper
        var helperType = typeof(ActionHelper<>).MakeGenericType(type);
        // create an instance of the helper
        // and upcast to base class
        var helper = (ActionHelper)Activator.CreateInstance(helperType);
        // call base method
        return helper.CreateActionImpl();

// Usage
// Note: The "var" is always "Delegate"
var @delegate = ActionHelper.CreateAction(anyTypeAtRuntime);

That said, I don't recommend using this method. Instead, use

Action<object> action = obj => Console.WriteLine("Called = " + obj);

It offers

  • The same functionality.

    Your original code "Called = " + obj" calls .ToString() on the parameter. So does the above.

  • No performance difference.

    If the obj parameter is a value type, both variants perform a boxing operation. The boxing in the first is not obvious, but "Called = " + obj" boxes value types.

  • Shorter and less error-prone.

share|improve this answer
"No performance difference" To what? you haven't provided any code that actually dynamically creates a delegate. You conveniently provide an abstract class with an abstract method CreateActionImpl but don't provide an implementation--the only method that would actually dynamically create the action. –  Peter Ritchie Sep 9 '12 at 13:40
@Peter I've explained the performance implications in the paragraph following the claim. Also, the OP wanted just the type parameter of the delegate late bound. Which the code does without Reflection.Emit. –  Mark Sep 9 '12 at 19:44

Your Answer


