Skip to content

02 Generating Delegate Types dynamically in C#

win edited this page Mar 9, 2015 · 1 revision

see the idea from stackoverflow

The simplest way would be to use the existing Func family of delegates.

Use

typeof(Func<,,,,>).MakeGenericType(...).

For example, for your int Del2(int, int, string, int) type:

using System;

class Test
{
    static void Main()
    {
        Type func = typeof(Func<,,,,>);
        Type generic = func.MakeGenericType
            (typeof(int), typeof(int), typeof(string),
             typeof(int), typeof(int));
        Console.WriteLine(generic);
    }
}

If you really, really need to create a genuinely new type, perhaps you could give some more context to help us help you better.

answered Apr 21 '09 at 15:22 Jon Skeet 758k33150836374