Dynamic Injector Mechanism
The compilation model for dynamic languages enables you to use simple syntax into which additional code is injected at run time. For example, you might have code that reads a value from the query string. The URL for the page might look like this:
http://someserver/somepage.aspx?MyValue=17
In a C# page, you can use the following code to obtain the value:
String myValue = Request.QueryString["MyValue"];
The dynamic injector mechanism allows you to write much simpler code in dynamic languages, as shown in the following examples:
myVar = Request.MyValue
The support code for dynamic languages enables the registration of an object referred to as an injector. By registering as an injector, the object agrees to handle code that matches a certain pattern. For example, if the dynamic language engine is unable to resolve the expression SomeObj.SomeName
, where SomeObj
is an HttpRequest
object and SomeName
is not a real property of the HttpRequest
object, the engine calls the injector that has been registered to handle that pattern. The injector handles the expression by calling SomeObj.QueryString["SomeString"]
. The net effect is exactly the same, but the syntax is much cleaner.
The injector mechanism has many potential uses. For example, wherever you would write SomeControl.FindControl("SomeChildControl")
in C#, you can write SomeControl.SomeChildControl
in a dynamic language application. The mechanism is extensible and can be applied to any collection that is indexed by strings.
No comments:
Post a Comment