Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3845992
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:19:45+00:00 2026-05-19T16:19:45+00:00

Short question, Is there a way in .NET 4.0 to take a string that

  • 0

Short question,
Is there a way in .NET 4.0 to take a string that represents the method body, and compile it into a Func/Action, or is there a library to do so?

Clarification:

I need something that will not generate any dll, it needs to be completely dynamic, something like eval() in javascript. I need to convert string into a Func/Action without creating dll.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:19:46+00:00Added an answer on May 19, 2026 at 4:19 pm

    You can use the CSharpCodeProvider class to compile source code into an assembly.

    For example:

    var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
    var options = new CompilerParameters { OutputAssembly = path);
    var results = compiler.CompileAssemblyFromFile(options, sourceFile);
    

    To compile a single function, you can wrap it in a class with appropriate using statements to create a complete source file, then get a delegate using Reflection:

    var assembly = results.CompiledAssembly;
    var method = assembly.GetType("WrapperClassName").GetMethod("MethodName");
    var delegate = (Action)Delegate.CreateDelegate(typeof(Action), method);
    

    For a more complete example:

    static readonly Assembly[] References = new[] { typeof(Enumerable).Assembly, typeof(Component).Assembly };
    public Action CompileMethodstring source) {
        var options = new CompilerParameters(References.Select(a => a.Location).ToArray()) {
            GenerateInMemory = true
        };
        string fullSource = @"public static class HolderClass { public static void Execute() { \r\n" + source + "\r\n} }";
        try {
            var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
    
            var results = compiler.CompileAssemblyFromSource(options, fullSource);
    
            if (results.Errors.Count > 0)
                throw new InvalidOperationException(String.Join(
                    Environment.NewLine, 
                    results.Errors.Cast<CompilerError>().Select(ce => ce.ErrorText)
                ));
    
            return (Action)Delegate.CreateDelegate(
                typeof(Action),
                results.CompiledAssembly.GetType("HolderClass").GetMethod("Execute")
            );
        } finally { options.TempFiles.Delete(); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question in short: How tot do this in MVC.NET? Question in long version: Im
Both .NET Socket and NetworkStream offer Read method, which blocks, when there is no
This is sort of a follow-up to this question . If there are multiple
There have been a couple of questions that sort of dealt with this but
Long winded title, short question: If one wants to develop for Windows but not
The short form of this question: When, if ever, is it appropriate to use
Very basic question: how do I write a short literal in C++? I know
So, the context of this question is pretty specific, but I think there is
I feel kinda stupid to even ask that question, But i was sitting like
First off I'm not sure that's the proper title for this question but hopefully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.