Generating Delegate Types dynamically in C#

前端 未结 2 1560
面向向阳花
面向向阳花 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

提交回复
热议问题