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.

302 ❘ ChaPTer 12 dynAmic lAnGuAGe extensiOns<br />

<br />

<br />

<br />

The code defines a section for “microsoft.scripting” <strong>and</strong> set a couple of properties for the IronPython<br />

language engine.<br />

Next, you get a reference to the ScriptEngine from the ScriptRuntime. In the example, you specify that<br />

you want the Python engine, but the ScriptRuntime would have been able to determine this on its own<br />

because of the py extension on the script.<br />

The ScriptEngine does the work of executing the script code. There are several methods for executing<br />

scripts from files or from snippets of code. The ScriptEngine also gives you the ScriptSource <strong>and</strong><br />

ScriptScope.<br />

The ScriptSource object is what gives you access to the script. It represents the source code of the<br />

script. With it you can manipulate the source of the script. Load it from a disk, parse it line by line, <strong>and</strong><br />

even compile the script into a CompiledCode object. This is h<strong>and</strong>y if the same script is executed<br />

multiple times.<br />

The ScriptScope object is essentially a namespace. To pass a value into or out of a script, you bind a<br />

variable to the ScriptScope. In the example, you call the SetVariable method to pass into the Python<br />

script the prodCount variable <strong>and</strong> the amt variable. These are the values from the totalItems text box<br />

<strong>and</strong> the totalAmt text box. The calculated discount is retrieved from the script by using the GetVariable<br />

method. In this example, the retAmt variable has the value you’re looking for.<br />

In the CalcTax button you look at how to call a method on a Python object. The script CalcTax.py is a very<br />

simple method that takes an input value, adds 7.5 percent tax, <strong>and</strong> returns the new value. Here’s what the<br />

code looks like:<br />

def CalcTax(amount):<br />

return amount*1.075<br />

Here is the <strong>C#</strong> code to call the CalcTax method:<br />

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

{<br />

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

dynamic calcRate = scriptRuntime.UseFile("CalcTax.py");<br />

label6.Content = calcRate.CalcTax(Convert.ToDecimal(label5.Content)).ToString();<br />

}<br />

A very simple process. Again you create the ScriptRuntime object using the same configuration settings<br />

as before. calcRate is a ScriptScope object. You defined it as dynamic so you can easily call the CalcTax<br />

method. This is an example of the how the dynamic type can make life a little easier.<br />

dynamiCobjeCT <strong>and</strong> eXP<strong>and</strong>oobjeCT<br />

What if you want to create your own dynamic object You have a couple of choices for doing that, by<br />

deriving from DynamicObject or by using Exp<strong>and</strong>oObject. Using DynamicObject will be a little more<br />

work because you have to override a couple of methods. Exp<strong>and</strong>oObject is a sealed class that is ready<br />

to use.<br />

dynamicobject<br />

Consider an object that represents a person. Normally, you would define properties for the first name,<br />

middle name, <strong>and</strong> last name. Now imagine the capability to build that object during runtime, with<br />

the system having no prior knowledge of what properties the object may have or what methods the<br />

object may support. That’s what having a DynamicObject-based object can give you. There may be<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!