简体   繁体   中英

How to execute a c# code dynamically

I am working on C# 3.5 winforms project.

I want to execute some code dynamically, which is in a string variable. the code, I want to execute is something like this :

(GetSetting("MYSETT1") == 1? "OK" : "Cancel")

I want to use methods which are existed in my project and by using them I want to perform some task.

Is it possible dynamically ?

If you're after a "pure" Microsoft solution you should check out Roslyn, once it ships. But until then you might want to take a look at the Fast Lightweight Expression Evaluator project on CodePlex:

Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient. Try out the demo, which lets you generate images based on expressions, and see for yourself.

If that doesn't fit your bill you should check out the shameless self-promotion of my own project, below.

ExpressionEvaluator

ExpressionEvaluator is a library to help developers evaluate C# and VB .NET expressions. The expressions you need to evaluate are compiled through the .NET Framework's own CodeDOM so nearly all language features are supported. The library can expose remotable objects to the expressions for a scripting-like capability. All expression evaluation is sandboxed.

Example

static void Main(string[] args)
{
    var expressions = new List<string>
                            {
                                "3 * 5",
                                "Log10(50)",
                                "Parameters!Greeting + \" World!\""
                            };

    // An ExpressionMeta contains the expressions and extensions to be compiled.
    var meta = new ExpressionMeta("VisualBasic");

    // Add the expressions to be compiled.
    foreach(var expression in expressions)
        meta.AddExpression(expression);

    // Add the extensions to be compiled.
    var extension = new Dictionary<string, string> {{"Greeting", "Hello"}};
    meta.AddExtensionIgnoreAssembly(new Extension("Parameters", extension));

    // Compile the expressions
    using(var evaluator = meta.Compile())
    {
        // Evaluate the expression
        foreach(var expression in expressions)
            Console.WriteLine("{0}", evaluator.Evaluate(expression));
    }
}

Output

15
1.69897000433602
Hello World!

You can dynamically compile your code and execute.

This links may be helpful:

Using the CodeDOM

Dynamically executing code in .Net

Mono team makes it possible via

http://www.mono-project.com/CsharpRepl

If you want to use Microsoft's product, you will have to wait for Roslyn,

http://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM