298
❘
CHAPTER 14
InteractIng wIth the DLr
When the
Runtime.ExecuteFile()
method call returns, the C# application that executed the script
receives a
ScriptScope
object that it can use to interact with the application in various ways. This
ScriptScope
object, like the
ScriptRuntime
object, contains all the usual defaults. It’s a good idea
to examine both
Runtime
and
Scope
in the debugger to see what these objects contain because you’ll
find useful information in both.
The script is running in a host application. In fact, they share the same console window. To show
how this works, the example writes output to the console window. It retrieves the
__name__
prop-
erty from
Scope
and displays it onscreen with the message, as shown in Figure 14-5. The point of
this example is that the IronPython script truly is hosted and not running on its own. The technique
shown here lets you perform simple interactions between C# or Visual Basic.NET and IronPython.
FIgURE 14-5:
The output shows that the host and the IronPython environment share the same console.
You may have heard that hosting DLR requires that you create entries in
the
App.CONFIG
file. While it’s true that you gain additional flexibility using the
App.CONFIG
file approach, it’s not true that you must use this approach to execute
scripts. Many developers will find that their scripts execute just fine using the tech-
nique shown in this section and with far less code than using the
App.CONFIG
file
approach. However, Chapter 15 does demonstrate this technique so that you can
see the best of both worlds when it comes to working with scripts.
UNDERSTANDINg THE ExTENSIONS TO LINQ ExPRESSION TREE
Part of the premise behind DLR is that every .NET language eventually ends up in Microsoft
Intermediate Language (MSIL) form. Whether you use C# or Visual Basic.NET, or even managed
C++, the output from the compiler is MSIL. That’s how the various languages can get along. They
rely on MSIL as an intermediary so that managed languages can work together.
Earlier versions of IronPython let you compile your application code using
the
–X:SaveAssemblies
command line switch. For example, you could type
IPY –X:SaveAssemblies Test.py
and press Enter to create an executable
file (it still requires that you supply IronPython.DLL). The latest version of
IronPython doesn’t supply this feature, so you can’t actually see IronPython
turned into MSIL.
548592c14.indd 298
2/24/10 12:49:07 PM
www.finebook.ir
Do'stlaringiz bilan baham: |