15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

300 ❘ ChaPTer 12 dynAmic lAnGuAGe extensiOns<br />

The CallSite is a type that h<strong>and</strong>les the lookup at runtime. When a call is made on a dynamic object at<br />

runtime, something has to go <strong>and</strong> look at that object to see if the member really exists. The call site caches<br />

this information so the lookup doesn’t have to be performed repeatedly. Without this process, performance<br />

in looping structures would be questionable.<br />

After the CallSite does the member lookup, the CallSiteBinder is invoked. It takes the information from<br />

the call site <strong>and</strong> generates an expression tree representing the operation the binder is bound to.<br />

There is obviously a lot going on here. Great care has been taken to optimize what would appear to be a<br />

very complex operation. It should be obvious that while using the dynamic type can be useful, it does<br />

come with a price.<br />

hosTing The dlr sCriPTrunTime<br />

Imagine being able to add scripting capabilities to an application. Imagine passing values in <strong>and</strong> out<br />

of the script so the application can take advantage of the work that the script does. These are the kind of<br />

capabilities that hosting the DLR’s ScriptRuntime in your app gives you. Currently, IronPython, IronRuby,<br />

<strong>and</strong> JavaScript are supported as hosted scripting languages.<br />

With the ScriptRuntime, you have the capability of executing snippets of code or a complete script stored<br />

in a file. You can select the proper language engine or allow the DLR to figure out which engine. The script<br />

can be created in its own app domain or in the current one. Not only can you pass values in <strong>and</strong> out of the<br />

script, but you can call methods on dynamic objects created in the script.<br />

With this amount of flexibility there are countless uses for hosting the<br />

ScriptRuntime. The following example demonstrates one way that<br />

the ScriptRuntime can be used. Imagine a shopping cart application.<br />

One of the requirements is to calculate a discount based on certain<br />

criteria. These discounts change often as new sales campaigns are<br />

started <strong>and</strong> completed. There are many ways of h<strong>and</strong>ling such a<br />

requirement; this example shows how it could be h<strong>and</strong>led using the<br />

ScriptRuntime <strong>and</strong> a little Python scripting.<br />

For simplicity, the example is a Windows client app. It could be part of<br />

a larger web application or any other application. Figure 12-1 shows a<br />

sample screen for the application.<br />

The application takes the number of items <strong>and</strong> the total cost of the items<br />

<strong>and</strong> applies a discount based on which radio button is selected. In a real<br />

application the system would use a slightly more sophisticated way to<br />

determine the discount to apply, but for this example, the radio buttons<br />

will work.<br />

Here is the code that performs the discount:<br />

figure 12-1<br />

private void button1_Click(object sender, RoutedEventArgs e)<br />

{<br />

string scriptToUse;<br />

if (CostRadioButton.IsChecked.Value)<br />

{<br />

scriptToUse = "AmountDisc.py";<br />

}<br />

else<br />

{<br />

scriptToUse = "CountDisc.py";<br />

}<br />

ScriptRuntime scriptRuntime = ScriptRuntime.CreateFromConfiguration();<br />

ScriptEngine pythEng = scriptRuntime.GetEngine("Python");<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!