Walkthrough: Dynamic Programming in Visual Basic 10.0 and C# 4.0 (Lisa Feigenbaum)

Lisa Feigenbaum

This blog post is part of a series on the walkthroughs included in the October VS2010 CTP. Each walkthrough guides you through a series of steps, to help you experience the new features coming up in Visual Studio 2010 and .NET Framework 4.0. Even if you choose not to download the CTP, you may find that you are still able to give feedback based on the descriptions and illustrations below. Please leave your feedback on this feature set at the end of this post, or at the following forum:

http://social.msdn.microsoft.com/Forums/en-US/vs2010ctpvbcs/thread/afb90240-1bc1-4191-b48f-d5e3a218b520

Thanks!
Lisa

Walkthrough: Dynamic Programming in Visual Basic 10.0 and C# 4.0

This walkthrough shows how the new Dynamic feature in Visual Basic 10.0 and C# 4.0 enables the natural use of object models that expect their host language to provide dynamic dispatch. This capability is demonstrated for two scenarios:

  • Silverlight, demonstrating access to browser object models that expect JScript.
  • IronPython, demonstrating access to libraries that are written for dynamic languages. Note that more advanced IronPython scenarios that involve hosting your own scripts with access to your application’s object model are beyond the scope of this walkthrough.

Note: The use of Dynamic with Office programming is demonstrated in the Office Programmability walkthrough.

Important: For this CTP release, the Dynamic feature is only functional when used from C# 4.0. The Visual Basic 10.0 code shown below will not work at this time. It provides a preview of what we expect the Dynamic feature to look like in Visual Basic when Visual Studio 2010 is released.

Prerequisites:

The Silverlight scenario uses the following resources:

  • Silverlight 2 RTW runtime
  • Silverlight 2 RTW SDK
  • An administrator account. You must run Visual Studio 2010 as an administrator to work around a known issue with the Visual Studio 2010 C# compiler’s output of the assembly metadata expected by Silverlight 2.

The IronPython scenario uses the following resources:

Silverlight Scenario

A developer wants to integrate the Virtual Earth JScript control into an existing Silverlight application. The developer wants to script the control by using the new Dynamic feature that is available in C# 4.0 and Visual Basic 10.0. The application already displays a list of photos that are available on Flickr for a given subject. The developer wants to display these images in a way that also enables the user to see where the pictures were taken.

To set up the Silverlight project

1. Download the SilverlightStarter project as part of the Visual Studio 2010 CTP 1 Samples (available here). You must use this specific project because it includes some binaries that are necessary to enable the Dynamic feature in Silverlight 2 for the referenced types in the CTP 1 Virtual PC image.

2. Extract the SilverlightSolution folder to your C: root folder. If you choose a different location, you will need to change a project setting after you open the solution to run the application.

3. Open the Visual Studio 2010 CTP 1 as an administrator. On Windows Vista or Windows Server 2008, you must right-click the Visual Studio 2010 icon and click Run as administrator. Because of limitations of the CTP project that we are using to demonstrate the Dynamic feature with Silverlight 2, you must be running as an administrator when you compile the application. This will not be required with the release version of Visual Studio 2010 and future versions of Silverlight.

4. Load the SilverlightSolution.sln solution from the extracted SilverlightSolution folder.

5. If you chose to extract the SilverlightSolution folder to a location other than the C: root folder, you must change the project’s Start Action to load the correct .htm file when you run the application. To do this, right-click the Silverlight Solution project in Solution Explorer and click Properties. On the Debug tab, and change the Start browser with URL path to be the location of StartPage.htm in your solution folder. For example, if the path to SilverlightSolution is D:SilverlightSolution, the URL would be D:SilverlightSolutionSilverlightProjectbinDebugStartPage.htm.

6. Build and run the application. Click Yes to continue debugging if prompted. You should see a Silverlight application in a browser. (The browser might appear behind the Visual Studio window.). You can click Get All to retrieve a list of photos, but you cannot yet see the images. Your Web page should look like the following illustration:

clip_image002

If the browser shows a blank white page, make sure that you are running Visual Studio 2010 as an administrator and that you have adjusted the project’s Start Action to point to the right URL for StartPage.htm, as described above.

To enable dynamic access to the HTML DOM

7. Add the following code to the top of the Page class:

image

Notice that the variables are typed as dynamic. Expressions of type dynamic enable you to access arbitrary methods and properties that are resolved at run time instead of at compile time. This is known as dynamic dispatch (in Visual Basic, it is called late binding) and it allows concise access to the members of objects without static types that indicate what members they support, as is the case with JScript objects.

Also notice the call to the .AsDynamic extension methods on HtmlDocument and HtmlWindow, each of which return a wrapper that implements IDynamicObject. IDynamicObject is the interface that informs the dynamic dispatcher how to dispatch operations that are performed on an object. In this case, the wrapper is indicating that you actually want to pass method invocations and field access through to the JScript object model of the browser. In future releases, classes such as HtmlDocument and HtmlWindow will si mply implement IDynamicObject themselves, letting you skip these wrappers and the .AsDynamic extension methods that return them.

8. Add the following code to the top of the btnSearch_Click method:

image

To add the Virtual Earth JScript control

9. In Solution Explorer, double-click the StartPage.htm file in the binDebugSLDemo folder to edit the source code.

10. Add the following line above the <title> tag:

image

This includes the script for the Virtual Earth JScript control.

11. Add the following line just above the </body> tag:

image

This sets up a <div> where the control will be located.

That’s it! These are all the modifications that need to be made to the HTML. You can now instantiate the Virtual Earth control and manipulate it from C# code, not JScript, even though the <div> that will host it resides directly within the HTML page, outside of Silverlight.

To script Virtual Earth from C#

12. In Solution Explorer, double-click the Page.xaml.cs file to edit the source code.

13. Just above the definition for the nested class Item, add the following code to the body of the Page class:

image

Note: In C# 3.0, where doc, win, and map would have static types, the following code would be required for the body of AddPin, which includes an explicit cast from Object and method invocations that require the name of the method to be passed as strings:

image

16. Build and run the project. You should now see a map on the right side of the application. When you click an item in the list, you should see a pushpin on the map. Rest the mouse pointer over the pushpin to cause an image and text to appear, as shown in the following illustration:

clip_image004

IronPython Scenario

This walkthrough demonstrates how to access an IronPython library from C#.

To set up an IronPython interop project

1. Start Visual Studio 2010 and create a new C# console application.

2. Add references to IronPython.dll, IronPython.Modules.dll, and Microsoft.Scripting.dll (all located in the %PROGRAMFILES%IronPython 2.0 VS10 CTP folder).

3. Copy the Python Lib folder into the binDebug folder for your project. (For example, copy the %PROGRAMFILES%IronPython 2.0 VS10 CTPLib folder into the C:IronPythonProjectbinDebug folder.)

4. Add the following using statements to the top of the Program.cs file:

image

To import an IronPython module in C#

5. Add code to create a new ScriptRuntime to represent the hosted IronPython environment. This ScriptRuntime can then be used to load Python code, in this case the library module random, which contains the shuffle function.
Add the following code to the Main method:

image

7. Build and run the application. The Python module random repeatedly shuffles the numbers 1 through 7 in random order. Notice that there is a startup cost when the Python runtime is first initialized, but that each iteration of the loop then executes extremely fast.

0 comments

Leave a comment

Feedback usabilla icon