3

I have to develop an application that can execute some functions from a user created text files.

I searched and found some threads :

Execute code lines from a text file in C#

Execute C# code in a String?

How to compile a C# file with Roslyn programmatically?

But I did not find what I was looking for ...

I don't want to execute input string codes as external or separated name spaces ...

what I'm looking for is executing some part of main codes from a string value

Example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;


namespace ConsoleApp1
{

    public partial class Window : MainWindow
    {
        string internal_message = "Hello World!"; /// a value set in main code
        public UserControl1()
        {
            InitializeComponent();
        }


     //////////////////////////////////// I want this part be executed from a string [
    void Saysomething()
    {

            MessageBox.Show(internal_message); /// get internal message from main values
        }

//////////////////////////////////// ]

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Saysomething(); //// run function executed from text data
        }
    }
}

My functions are complex and I need a general solution in 3ds max maxscript there's a function we call "Execute"

it takes string and execute it as dynamic code in same code and namespace :

myvalue = "val = 30";
execute(myvalue);
print val;

/// result 
30

I need the same in c# And please do not use roslyn as much as possible.

  • 3
    Javascript is an interpreted language, so executing code dynamically is relatively easy. C# is a compiled language, it's a lot harder. Those linked questions **are** the way of doing it. – hnefatl Apr 08 '18 at 20:50
  • 1
    "do not use roslyn as must as possible" - then this question is basically a non-starter. Roslyn is the compiler and it is how you will dynamically compile code. In fact there is an entire package "Microsoft.CodeAnalysis.CSharp.Scripting" that is specifically geared towards making scripting uses cases easy. Perhaps you are under the impression you need to manage some complex external compilation assemblies. With roslyn's scripting package, it's basically a one-liner: `CSharpScript.EvaluateAsync(@"Console.WriteLine(""Hello World"");", ScriptOptions.Default.AddImports("System"))`. – Mike Zboray Apr 08 '18 at 21:13
  • `And please do not use roslyn as much as possible.` Can you provide some context as to why you **don't** want to use Roslyn? – mjwills Apr 08 '18 at 21:23
  • @mjwills because my runtime version doesn't support it and it's very heavy to embed in my application . – BattleReloaded Apr 08 '18 at 22:01

0 Answers0