Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

scripting

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;
    }
like image 886
Raghavendra Kumar Avatar asked Apr 20 '13 03:04

Raghavendra Kumar


People also ask

What is this C tutorial for?

This tutorial is designed for software programmers with a need to understand the C programming language starting from scratch. This C tutorial will give you enough understanding on C programming language from where you can take yourself to higher level of expertise.

What are the uses of C programming language?

C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C are - Operating Systems. Language Compilers. Assemblers. Text Editors

What is the use of using statement in C?

The "Using" Statement in C#. The using statement in C# defines a boundary for the object outside of which, the object is automatically destroyed. The using statement is exited when the end of the "using" statement block or the execution exits the "using" statement block indirectly, for example - an exception is thrown.

What is the use of @C++ using keyword?

C++ using is one of the keywords that is used for to bring the some specific member functions from the namespace that can be started using the keyword. It will be the directive, declarative, and other even directives are also implemented.


1 Answers

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.

like image 134
Zenexer Avatar answered Nov 01 '22 04:11

Zenexer