Note: This code has been updated using Silverlight 2 RTM
With Silverlight 2 a new set of components have been introduced into the equation, the most important one is the CLR Execution engine, that support in this case the dynamic language runtime (DLR) that we are going to host in this post. The following chart shows the items that have been changed since version 1.0
This post will focus on hosting the scripting engine in your Silverlight application adding dynamic code to the execution, allowing the inclusion of instances into the scripted code. The first thing that we need to do is including the namespaces:
using Microsoft.Scripting.Hosting;using Microsoft.JScript.Runtime;
ScriptRuntime MyRuntime = JScript.CreateRuntime();
With the instance in place now we can retrieve the script engine as follows:
ScriptEngine JSEngine = MyRuntime.GetEngine("js");
IScriptScope Scope = JSEngine.CreateScope();
Each execution scope represent the environment where your code will run, it allows you to set variables and their values. We can add an object instance that the scripted code will run (similar to executing COM components in the past but now linked to your silverlight object). Let’s suppose that I expose a method that calls a WCF method:
IRemoteInterface TransparentProxy = MyFactory.CreateChannel();Scope.SetVariable("WCFProxy", TransparentProxy);
Now that we have the scope, we can start defining our code. In order to use the code we should use the ScriptSource object.
ScriptSource Code = JSEngine.CreateScriptSourceFromString(“WCFProxy.MyMethod();”);
The script engine allows you to load external files as well, just explore the options that it offers. The ScriptSource object allows you to play with the code, like getting individual lines. All this information can be dynamically changed, giving you the flexibility that you need. Now, the final step, we can compile the code to make sure that is correct and execute it (note that you can execute it directly and will be compiled on the fly).
source.Compile() ; // OptionalCode.Execute(Scope);
You can shutdown the engine when is not needed using the Shutdown() command. You can see the power of injecting code into your application on the fly and the simplicity of the model. Explore the DLR!