简体   繁体   中英

Using C# as a scripting language for a C# application

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;
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM