Why would you want to do that?
- Configuration (my post about that with IronPython)
- JSON over XML to implement configuration file.
- Configuration file implemented with a dynamic language can execute code.
- Configuration file can contain object instances.
- Plug-in architecture
- Plug-ins can be updated with notepad on the fly or not
- Script-able applications
Implementation
I wrote a library which I may name DynamicJavaScript.Net, which allow to access the JavaScript objects and arrays using the dynamic feature of C# 4 (see my post here).
A simple configuration file in JavaScript
function User(userName, firstName, lastName) { this.UserName = userName; this.LastName = lastName; this.FirstName = firstName; } var Configuration = { Server : 'TOTO', Database: 'Rene', Debug : true, Users : [ { UserName : 'bpascal', FirstName: 'blaise', LastName : 'pascal' }, new User('cmontesquieu', 'charles', 'montesquieu') ] }
The C# source code to load the configuration
dynamic csContext = new DynamicJavascriptContext( new JavascriptContext() ); csContext.Run(System.IO.File.ReadAllText("configuration.js")); var server = csContext.Configuration.Server; var databse = csContext.Configuration.Database; var debug = csContext.Configuration.Debug; for(var i=0; i<csContext.Configuration.Users.Length; i++) { var userName = csContext.Configuration.Users[i].UserName; }
No comments:
Post a Comment