0

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 ?

8
  • 2
    Make it as method and call it. what is dynamic here? Commented Mar 3, 2012 at 10:54
  • just like this code i have hundreds of commands, so to make lots of methods for this is not a very good solution. Commented Mar 3, 2012 at 10:57
  • 1
    For dynamic code generation, you can use CodeDOM or Emit or ... but, for your case, I think it's just a configuration settings, you can handle it with xml and some code, If you provide more detail about your problem we can help you better. Commented Mar 3, 2012 at 11:06
  • @SaeedAmiri I want to execute some code ... which i have in a string variable Please share your insights into marking System.Strings as methods. Commented Mar 3, 2012 at 11:07
  • 1
    e.g I want to know where parameters of your methods like: GetSetting("MYSETT1") == 1 initialized, and what's your usage from "OK" or "Cancel" result, why you don't use GetSetting("MYSETT1") result? and where you store this hundreds methods? why do not put them in code instead of storing somewhere else? Commented Mar 3, 2012 at 11:21

3 Answers 3

1

You can dynamically compile your code and execute.

This links may be helpful:

Using the CodeDOM

Dynamically executing code in .Net

Sign up to request clarification or add additional context in comments.

2 Comments

how can i use the methods which is in my project with this dynamic code ?
i have found my solution : in CodeDOM . to execute methods in class in my project i have to add that assembly.
1

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!

Comments

0

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

2 Comments

Whatever is new and shiny does erase all memory of what was before.
You should say whatever comes from Microsoft erases all memory of what was free and open source. It was NAnt/NDoc/NUnit/NHibernate ... (a very long list), but now what we get from Microsoft is quite limited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.