0

I am developing an Desktop Application in WPF using C# .

For the sake of simplicity, Assume my Application has functions which draw lines in said direction goleft() , goright() , goforward() , goback() .

when any of these function is called a line of one inch will be drawn on screen.

I want to make application where user will write code in a file in any editor (say notepad) and save that file in some fixed format (say .abc or .xyz)

Imaginary Example :

   (section_start)

   For(int i = 0 ; i<= 20 ; i++ ) 
    {
      if(i<5)
      goforward();

      else if(i==5)   
       goleft();

      else if(i < 10)
       forward();
        .......
        ........
    }

    (section_End)

Can i make application which should be capable of reading this file and execute code which is written in between of (section_start) and (section_End). and only if possible can check for syntax errors too... (Not compulsory).

Please guide me on this issue :

Disclosure : My actual Application is somewhat else and could not discuss here due to my company's rules.

Thanks to all who replied to my question. Stackoverflow is fantastic site , i have found the roadmap where to go , till today morning i did not have any clue but now i can go ahead , thanks all of you once again Will ask question again if i get stucked somewhere

Nidhi Sharma
  • 135
  • 1
  • 2
  • 12
  • Sounds like compiling into a assembly in memory at runtime. – Myrtle Jul 20 '12 at 09:19
  • Do you want a **Parser/Interpreter** or a **JIT Compiler**? – Alvin Wong Jul 20 '12 at 09:24
  • Similar Question: http://stackoverflow.com/questions/11576463/can-i-read-code-from-a-file-and-and-let-that-code-run-in-an-application – Rutesh Makhijani Jul 20 '12 at 09:25
  • 1
    @RuteshMakhijani Are you referring to the same question? – Alvin Wong Jul 20 '12 at 09:25
  • OP needs to define requirement of parsing or compiling. Either way, there's plenty of resources out there for both. – Simon Whitehead Jul 20 '12 at 09:25
  • @Alvin Wong The question referred is about similar functionality and contains solution which can work in this scenario also – Rutesh Makhijani Jul 20 '12 at 09:27
  • 1
    @RuteshMakhijani what? It's the exact link to *this* question – Alvin Wong Jul 20 '12 at 09:28
  • @AlvinWong , I have no idea this requirement came to me today only , Please guide me which is suitable . and show me the road map please – Nidhi Sharma Jul 20 '12 at 09:30
  • @RuteshMakhijani , please post the link to that question because you have gave link above of this page only – Nidhi Sharma Jul 20 '12 at 09:32
  • @Nidhi Sharma - sorry for the mix up, please find the link to the question I was referring http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – Rutesh Makhijani Jul 20 '12 at 13:06
  • @NidhiSharma - I dont know about ur exact requirement, is it required to read code from external file, compile and execute? If yes then you can refer to the inputs and build the code. But if your requirement is to let users specify the diagram elements - referring to points you have mentioned - and not the C# code (i.e. requirement does not mandate users to write C# code - it is one of the implementation option you are evaluating), then I would suggest use XML file or any other notation to accept inputs from file, parse the file and execute the approriate actions in your code – Rutesh Makhijani Jul 20 '12 at 13:30

5 Answers5

2

You can read the file content using FileInfo and get the code you need to execute.

Then you can execute the code using CSharpCodeProvider like in this post:

using (Microsoft.CSharp.CSharpCodeProvider foo = 
       new Microsoft.CSharp.CSharpCodeProvider())
{
var res = foo.CompileAssemblyFromSource(
    new System.CodeDom.Compiler.CompilerParameters() 
    {  
        GenerateInMemory = true 
    }, 
    "public class FooClass { public string Execute() { return \"output!\";}}"
);

var type = res.CompiledAssembly.GetType("FooClass");

var obj = Activator.CreateInstance(type);

var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
}
Community
  • 1
  • 1
Mohamed Ramadan
  • 793
  • 5
  • 15
0

This is called scripting your application if I understand correctly. C# does not support this out of the box. One thing to look into could be the new Roselyn compiler from Microsoft (it is a new take on the C# compiler, which lets you do just this).

For more info on Roselyn check out: http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx

I've only seen a demo of it, but it looks very promissing, and should solve your problem.

Simon Ejsing
  • 1,455
  • 11
  • 16
0

It's not clear what kind of code you want compiled but here is a guide on how to compile code code with C#.

Icy Creature
  • 1,875
  • 2
  • 28
  • 53
0

You can choose CodeDOM or IL Emit

More help on CodeDOM

More information on IL Generator / Emit

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
0

You could use IronPython to handle the script.

Here's an example of how to do this:

First you need a navigation object to perform the operations on:

public class NavigationObject
{
    public int Offset { get; private set; }

    public void GoForwards()
    {
        Offset++;
    }

    public void GoBackwards()
    {
        Offset--;
    }
}

Then the code to execute the file:

public void RunNavigationScript(string filePath, NavigationObject navObject)
{
    var engine = Python.CreateEngine();
    var scope = engine.CreateScope();

    scope.SetVariable("navigation", navObject);
    var source = engine.CreateScriptSourceFromFile(filePath);
    try
    {
        source.Execute(scope);
    }
    catch(Exception
    {
    }
}

The script file can then take the form of something like:

for x in range(0,20):
    if x == 5:
        navigation.GoBackwards()
    else:
        navigation.GoForwards()
Lukazoid
  • 19,016
  • 3
  • 62
  • 85