Click here to Skip to main content
15,897,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DissTest1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
           String s="Console.WriteLine(\"Hello World!22\");";
           Console.WriteLine("Hello World!11");
            \\Run s here
           Console.WriteLine("Hello World!33"
);

        }
    }
}
Posted
Updated 12-Sep-15 10:47am
v2

The answer is no, not with C++ or C#.

To do this, you need to embed a runtime parser/compiler in your program and you need to have provision in the language to execute the result of compilation.

You will certainly find libraries that do this but certainly not in C++ because you will have problems to access local variables.

In fact, only semi-compiled languages (using some p-code) can do what you want. And the feature must be planned from the conceiving. You get this feature in VisualFoxPro and other languages from the same family.

In xBase family languages: FoxPro/dBase/xHarbour...
VB
s="? 'Hello World!22'";
?"Hello World!11"
$ s
? "Hello World!33"

the $ calls the internal compiler on fly at runtime.

xHarbour comes with an utility "xPrompt" (open source) that is an interactive console app which embed a full featured parser/compiler. it can load and run a full program that is in a file as source code.
 
Share this answer
 
You typically execute strings using a script language. There are several scripting languages embeddable in a C# application, see "Scripting language for embedding into C#/.NET applications?" at Stack Overflow[^].
Another approach could be using CodeDom, see, for instance this Code Project article: "Dynamic Code Integration with CodeDom" [^].
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900