5

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 would be simple types.

eg, we need to generate

int Del(int, int, int, string)

and

int Del2(int, int, string, int)

Any pointers on how to get started on this would be very helpful.

We need to parse formulate which are represented as xml.

For example, we represent (a + b) as

<ADD>
    <param type="decimal">A</parameter>
    <param type="decimal">B</parameter>
</ADD>

We now want this to be exposed as Func<decimal, decimal, decimal>. We of course want to allow nested nodes in the xml, e.g:

(a + b) + (a - b  * (c - d)))

We want to do this using expression trees and Expression.Compile.

Suggestions on the feasibility of this approach are welcome.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
SharePoint Newbie
  • 5,974
  • 12
  • 62
  • 103

2 Answers2

11

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • AB is an example. We want to create a callable method from this at runtime. – SharePoint Newbie Apr 21 '09 at 18:05
  • I'm afraid you haven't really given us enough information about what either the problem is or the approach to let us judge whether it's a good idea or not. Using expression trees in general sounds like a reasonable approach, but I'm not quite sure how the rest of your question fits into that. – Jon Skeet Apr 21 '09 at 18:06
  • We are trying to parse the XML recursively and generate an expression tree. We are stuck with generation of a callable method from the expression tree. We would not want to use LCG and emit opcodes ourselves. Generating a delegate for each node and then chaining the delegates is also an option. – SharePoint Newbie Apr 21 '09 at 18:14
  • Is it possible to create an expression tree tree using Expression recursively for every node dynamically and then do a .Compile on it? For example ABC, should lead to Func. – SharePoint Newbie Apr 21 '09 at 18:17
  • We need to do this but dynamically. http://stackoverflow.com/questions/346523/how-do-i-compile-an-expression-tree-into-a-callable-method-c – SharePoint Newbie Apr 21 '09 at 18:22
  • So long as you can keep track of which delegate type you want to convert to at the end, you should be able to do it all with just Expression (untyped) and then use Expression.Lambda as the last step. – Jon Skeet Apr 21 '09 at 18:45
  • Which is why we need to generate a delegate type dynamically. Any pointers to this would be helpful. Thx for the help. – SharePoint Newbie Apr 21 '09 at 18:48
  • Can you specify a maximum number of parameters? If so, just make sure you've got enough Func<...> delegates (writing your own if necessary) and use the code I've written here. – Jon Skeet Apr 21 '09 at 19:13
-1

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

Community
  • 1
  • 1
x0n
  • 51,312
  • 7
  • 89
  • 111