Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Dynamically Executing Code in .NET : Page 4

Dynamic code execution is a powerful feature that allows applications to be extended with code that is not compiled into the application. Users can customize applications and developers can dynamically update code easily. Learn what it takes to execute code dynamically with the .NET Framework and create a class that simplifies these tasks by wrapping the details of the process in an easy-to-use interface that requires only a few lines of code.

Building an ASP-like Script Parser
Figure 2: The wwASPScripting class in conjunction with the wwScripting class can run C#-based script code that works with basic ASP syntax.
To show you how useful dynamic code execution is and how little code it takes to build powerful functionality, I've included another class called wwASPScripting and a small sample app that demonstrates it with the source code. It's basically a simple ASP template parser you can use in your own non-Web applications. Although ASP.NET has a powerful script parser, it unfortunately only works with Web interfaces, not for general code (See Sidebar: Why Do We Need a Script Parser?).

It isn't difficult to build a basic parser that can handle this task. Take a look at Figure 2, which shows both the generated C# code and the output.

If you look closer at Figure 2 you can see what happens behind the scenes. The HTML template is turned into C# source code. The parser simply runs through the page finding all of the <% tags and inserts the appropriate Response.Write() or Response.oSb.Append() commands. Non-tagged text is expanded into strings delimited with quotes. As a special case the <%@ %> directive handles Assembly and Import keywords to allow importing namespaces and assembly files for linking. To include assemblies and namespaces you can use directives like this:

<%@ Assembly name="System.Windows.Forms.dll"%> <%@ Import namespace="System.Windows.Forms"%>

I put together a separate class, wwASPScripting, to handle parsing strings into C# code. It's only a demo and provides rudimentary functionality—a first stab. This parser also only handles C# code at this time as VB code would require generating code quite differently and my VB skills lack a bit in that department.

Figure 6 shows the code to accomplish parsing a template page.

The key and new feature of this code is the ParseScript method that basically turns the ASP-style code seen in Figure 2 into runnable C# code that is then passed to the wwScripting class to dynamically execute.

The wwASPScripting class is only a first shot and doesn't do more than parse. It has a private implementation of a Response object that is used to write output into the output stream. The wwASPScripting class natively uses a string builder to allow output to be sent to a string or stream. The ParseScript method is rather short and you can review the source code of how the code conversion is performed in the wwAspScripting.cs source file included with the downloadable code.

You're So Dynamic
It is interesting how .NET allows you to run dynamic code—essentially it provides you all the tools that a compiler uses to generate an executable. If you want to get even more low level you can use the System.Reflection.Emit namespace to generate IL level code directly. I am amazed how little overall code this mechanism requires even if coming up with that code wasn't quite so trivial, digging through the .NET docs (and help from several people on various newsgroups!). It's also interesting to see how to apply this technology and build a custom script parser with even less code. The process is relatively easy and straightforward once you can use the wrapper classes. Well, easy may be a little overstated. This whole exercise requires deployment of two DLLs in your applications—the wwScripting DLL that holds both the code execution and scripting classes as well as the remote loader DLL required to handle the AppDomain proxy Interface. I hope these classes and this discussion help you understand how to run dynamic code in .NET. I learned a lot about how .NET works under the covers and I hope this article and the provided helper classes are useful to you in extending your applications with dynamic code. I cannot live without this capability in my applications.

Get the Code from this Article

Rick Strahl is president of West Wind Technologies on Maui, Hawaii. The company specializes in Web and distributed application development and tools with focus on Visual FoxPro, .NET and Visual Studio. Rick is author of West Wind Web Connection, a powerful and widely used Web application framework for Visual FoxPro and West Wind HTML Help Builder. He's also a Microsoft Most Valuable Professional, and a frequent contributor to magazines and books. He is co-publisher of CoDe Magazine, and his book, Internet Applications with Visual FoxPro 6.0, is published by Hentzenwerke Publishing. For more information please visit: www.west-wind.com/. .
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date