Using C# as a scripting language for a C# application -
i have developed application uses c# script files configurations , settings. script file contains various user generated objects , functions on objects. presently, user has generate .cs file using third party editor , supply path program make use of it. disadvantage method user not have flexibility of auto-complete , intellisense-esque support while editing script files.
i want embed script editing part application. can using rich-text editor. coding auto-complete part huge pain. there way in can provide user in-program editor auto-complete....
code compiling script dynamically in 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) { (int = 0; < res.errors.count; i++) { errors += "\n " + + ". " + res.errors[i].errortext; } } } catch (exception e) { throw (new exception("compilation failed exception!\n" + e.message + "\n compilation errors : \n" + errors + "\n")); } } return errors; }
specifically auto-complete, need make use of 2 systems: parser, , reflection.
a parser pretty straightforward concept, in theory, i'm sure won't easy write language syntactic sugar , many context-sensitive keywords c#.
since .net inherently reflective, , provides reflection framework, part shouldn't incredibly painful, either. reflection allows manipulate object-oriented elements comprising compiled assemblies--and assemblies themselves--as objects. method method
object, example. can take peek @ system examining members of type
class, provide 1 basic starting point reflection. useful starting point assembly
. msdn, usual, has wealth of "official" information in structured format.
Comments
Post a Comment