Generating Delegate Types dynamically in C#

前端 未结 2 1550
面向向阳花
面向向阳花 2020-12-01 20:19

We have a requirement where we need to generate delegate types on the fly. We need to generate delegates given the input parameters and the output. Both input and output wou

相关标签:
2条回答
  • 2020-12-01 20:30

    Jon's answer works fine if you're running framework 3.5 (but not everyone is).

    The 2.0 answer is to use Delegate.CreateDelegate(...)

    http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx

    A comparison of various ways to do this including Jon's Func, Delegate.CreateDelegate, DynamicMethods and various other tricks was discussed on an earlier thread:

    Delegate.CreateDelegate vs DynamicMethod vs Expression

    -Oisin

    0 讨论(0)
  • 2020-12-01 20:51

    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.

    EDIT: As Olsin says, the Func types are part of .NET 3.5 - but if you want to use them in .NET 2.0, you just have to declare them yourself, like this:

    public delegate TResult Func<TResult>();
    public delegate TResult Func<T, TResult>(T arg);
    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
    public delegate TResult Func<T1, T2, T3, TResult>
        (T1 arg1, T2 arg2, T3 arg3);
    public delegate TResult Func<T1, T2, T3, T4, TResult>
        (T1 arg1, T2 arg2, T3 arg3, T4 arg4);
    

    If 4 arguments isn't enough for you, you can add more of course.

    0 讨论(0)
提交回复
热议问题