none
I want to execute a line of C# code stored in a string RRS feed

  • Question

  • Thanks in advance for the help. What I want to do is

    I have a string

    String cS = "int i = varX + varY"
    
    

    What I want to accomplish is execute the int i = .... code, be able to set varX and varY from values set at run time. A basic idea is

    int VarX = 2;

    int varY = 3;

    Code.Execute(cS)

    and i will have 5 as its value. I also want to be able to use the value of i elsewhere.

    Monday, March 4, 2019 9:35 PM

All replies

  • See the following thread

    https://stackoverflow.com/questions/53844/how-can-i-evaluate-a-c-sharp-expression-dynamically


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 4, 2019 9:58 PM
    Moderator
  • Hi Espo4Life,

    Thank you for posting here.

    For your question, please try the code below. Please note that install Microsoft.CodeAnalysis.CSharp.Scripting from NuGet first.

    class Program
        {
            public static void Main()
            {
    
                CSharpScriptEngine.Execute(
                      //This could be code submitted from the editor
                      @" public class ScriptedClass
                {
                    public int i {get;set;}
                    public ScriptedClass()
                    {
                       int varX = 2;
                       int varY = 3;
                        i = varX + varY;
                    }
                }");
                //And this from the REPL
                Console.WriteLine(CSharpScriptEngine.Execute("new ScriptedClass().i"));
                Console.ReadKey();
            }
    
        }
        public class CSharpScriptEngine
        {
            private static ScriptState<object> scriptState = null;
            public static object Execute(string code)
            {
                scriptState = scriptState == null ? CSharpScript.RunAsync(code).Result : scriptState.ContinueWithAsync(code).Result;
                if (scriptState.ReturnValue != null && !string.IsNullOrEmpty(scriptState.ReturnValue.ToString()))
                    return scriptState.ReturnValue;
                return null;
            }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 5, 2019 7:05 AM
    Moderator
  • Thank you so much, just I probably needed to be more clear on my intent.

    What I want to do is store C# code to perform calculations in a database and when the code is read in from the database, replace the variables with the correct values and then use the result in the application itself.

    Thanks in advance for re-education me on this :)

    Tuesday, March 5, 2019 3:23 PM
  • Hi Espo4Life,

    I make a new sample. This could solve your question. Pleas try.

    class Roslyn_scripting
        {
            public static void Main()
            {          
                Method3();
            }
    
            public static void Method3()
            {
                var script = @"int Add(int x, int y) { return x+y; } Add(VarX, VarY)";
    
                int varX = 2;
                int varY = 3;
                var result = CSharpScript.EvaluateAsync<int>(script, null, new ScriptHost { VarX = varX, VarY = varY }).Result;
    
                //result is now 5
                Console.WriteLine(result);
                Console.ReadLine();
            }
    
        }
        public class ScriptHost
        {
            public int VarX { get; set; }
            public int VarY { get; set; }
        }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 6, 2019 7:16 AM
    Moderator