6

I have developed an application that uses c# script files for certain configurations and settings. The script file contains various user generated objects and certain functions on those objects. Presently, the user has to generate a .cs file using a third party editor and supply the path to my program to make use of it. The disadvantage with this method is that the user does not have the flexibility of Auto-complete and intellisense-esque support while editing the script files.

I want to embed the script editing part into my application. I can do that using a rich-text editor. But coding the auto-complete part is a huge pain. Is there any way in which I can provide the user with an in-program editor that also does auto-complete....

Code for compiling a script dynamically in a program.

public String Compile(String inputfilepath)
    {

        CompilerResults res = null;
        CSharpCodeProvider provider = new CSharpCodeProvider();
        String errors = "";

        if (provider != null)
        {
            try
            {
                Assembly asb = Assembly.Load("BHEL.PUMPSDAS.Datatypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=81d3de1e03a5907d"); 
                CompilerParameters options = new CompilerParameters();
                options.GenerateExecutable = false;
                options.OutputAssembly = String.Format(outFileDir + oName);
                options.GenerateInMemory = false;
                options.TreatWarningsAsErrors = false;
                options.ReferencedAssemblies.Add("System.dll");
                options.ReferencedAssemblies.Add("System.Core.dll");
                options.ReferencedAssemblies.Add("System.Xml.dll");
                options.ReferencedAssemblies.Add("System.Xml.dll");
                options.ReferencedAssemblies.Add(asb.Location);
                res = provider.CompileAssemblyFromFile(options, inputfilepath);
                errors = "";
                if (res.Errors.HasErrors)
                {
                    for (int i = 0; i < res.Errors.Count; i++)
                    {
                        errors += "\n " + i + ". " + res.Errors[i].ErrorText;
                    }
                }
            }

            catch (Exception e)
            {
                throw (new Exception("Compilation Failed with Exception!\n" + e.Message +
                    "\n Compilation errors : \n" + errors + "\n"));
            }

        }
        return errors;
    }
11
  • Lua, Python or powerscript? Which script language are you using? Commented Apr 20, 2013 at 3:43
  • 1
    C# itself is my scripting language. I am using the CodeDom namespace to compile C# script files on the fly from within my program. Commented Apr 20, 2013 at 3:44
  • oh my god! C# is your script? Are you sure? Commented Apr 20, 2013 at 3:45
  • 1
    Yes !! It's pretty simple to use too... Commented Apr 20, 2013 at 3:46
  • 5
    @David, please familiarize yourself with Roslyn. In particular: "and the upcoming support for scripting and interactive use of VB and C#." Commented Apr 20, 2013 at 3:48

1 Answer 1

2

Specifically for auto-complete, you will need to make use of two systems: a parser, and reflection.

A parser is a pretty straightforward concept, in theory, but I'm sure that it won't be easy to write for a language with as much syntactic sugar and as many context-sensitive keywords as C#.

Since .NET is inherently reflective, and provides a reflection framework, that part shouldn't be incredibly painful, either. Reflection allows you to manipulate the object-oriented elements comprising compiled assemblies--and the assemblies themselves--as objects. A method would be a Method object, for example. You can take a peek at this system by examining the members of the Type class, which provide one basic starting point for reflection. Another useful starting point is Assembly. MSDN, as usual, has a wealth of "official" information in a structured format.

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

2 Comments

Your method works on systems which are already compiled. What I want is Visual studio style Intellisense, which works even before the code is compiled.
Which essentially means you're compiling it as you go. Same idea. You can still use reflection; the framework is in place. You're basically compiling an object-oriented shell. You'll need to do something custom for local variables, but that's just one element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.