Click here to Skip to main content
15,123,540 members
Articles / Programming Languages / C#
Posted 26 Jan 2012

Tagged as


25 bookmarked

Execute lambda expression given as string

Rate me:
Please Sign up or sign in to vote.
4.85/5 (10 votes)
26 Jan 2012CPOL
How to execute lambda expression given as string


This code snippet enables you to read a lambda expression e.g. out of a database or a configuration file and executes it.

Using the code

You can use the code like this:

ExecuteLambdaExpression("int i = 5; MethodDelegate del = () => { 
       int j = 10; return j > i; }; bool val = del(); return val;"); 

If you want to use a Delegate with parameters, you should add another delegate to the "LambdaClass" given as string.


In a next version of this code, you can specify more delegates to be more flexible, or maybe a delegate with a parameter list.

delegate object MethodDelegate(params object[] args);

//ExecuteLambdaExpression("int i = 5;MethodDelegate del = () => {
//              int j = 10; return j > i; };bool val = del(); return val;")
private object ExecuteLambdaExpression(string lambdaExpression)
    string source =
    "namespace LambdaNamespace" +
    "{" +
        "public class LambdaClass" +
        "{" +
            "delegate bool MethodDelegate();" +
            "public bool LambdaMethod()" +
            "{" +
                lambdaExpression +
            "}" +
        "}" +

    CompilerParameters parameters = new CompilerParameters();
    parameters.GenerateExecutable = false;
    CSharpCodeProvider codeProvider = new CSharpCodeProvider();
    ICodeCompiler icc = codeProvider.CreateCompiler();
    CompilerResults results = icc.CompileAssemblyFromSource(parameters, source);

    Console.WriteLine("compiled with errors: " + 
        results.Errors.HasErrors + ", warnings: " + 

    if (!results.Errors.HasErrors && !results.Errors.HasWarnings)
        Assembly ass = results.CompiledAssembly;

        object lambdaClassInstance = 

        object lambdaMethodResult = 
                BindingFlags.InvokeMethod, null, lambdaClassInstance, new object[] { });

        return lambdaMethodResult;

    return null;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Member 8583845
United States United States
No Biography provided

Comments and Discussions

Generalyou will also get a deprecated code warning using the compil... Pin
GParkings13-Feb-12 0:57
MemberGParkings13-Feb-12 0:57 
GeneralBe aware, however, that every time that you compile and load... Pin
Juan__Perez30-Jan-12 11:06
professionalJuan__Perez30-Jan-12 11:06 
GeneralReason for my vote of 5 Very cool idea. I give you 5 because... Pin
Dean Oliver28-Jan-12 22:17
MemberDean Oliver28-Jan-12 22:17 
Reason for my vote of 5
Very cool idea. I give you 5 because of your sheer originality.
GeneralReason for my vote of 5 funny idea - I used similar code for... Pin
johannesnestler26-Jan-12 12:58
Memberjohannesnestler26-Jan-12 12:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.