Thursday, 15 July 2010
Article Index
Significant whitespace
Objects, classes and self
.NET interop


.NET interop

The whole point of using IronPython as opposed to just Python is that it promises access to .NET and what this really means is access to the .NET Framework.

You can create a range of project types but the basic principles of interop are the same in each. Lets start with a Windows Forms application - create a new project. You can't make use of the drag-and-drop designer to build a user interface so you have to create all of the objects needed and wire up interrupts manually.

To create a window with a textbox and a button you would use first create a form class that inherits from Form and define all of the controls needed:

import clr
from System.Windows.Forms import *
class MyForm(Form):
def __init__(self):
self.Button1 = Button()
self.Button1.Text = 'Click Me'
self.Textbox1 =TextBox()

The initial instructions load the assemblies and references we need to use. Next the class is defined and its constructor creates a Button and a Textbox. In a real example you would have to spend a lot more effort setting properties to achieve a reasonable layout.

To define a click event handler for the button we need to define the OnClick method:

def OnClick(source,event):

This simply has to have the correct number of parameters. Finally we need to create the instance of the MyForm class and start the whole application running:

form = MyForm()

Now the program will display the message in the textbox when you click on the button.

Notice that you can use the .NET objects just as if they were Python objects and the only real difference is that you have to remember to make use of the self keyword as appropriate.

To make use of any .NET classes you have to first import than using the import statement. Specifically you have to import the special System module that connects IronPython to the .NET classes. If you simply use import System you have to use fully qualified class names but you can also import namespaces so that you can use short names.

For example a standard Windows Forms project would need something like:

import System
from System.Windows.Forms import *
from System.ComponentModel import *
from System.Drawing import *
from clr import *

Once you have imported a .NET class you can use it in Python style, i.e. you don't need a "new" to create an instance. For example:

a=System.String("Hello World")

creates a .NET String which can be assigned to any .NET property that accepts a String.  If you don't want to use a fully qualified name then you can use:

from System import String
a=String("Hello World")

What is perhaps less obvious is that once you import a .NET class it can mix with the pure Python classes.

For example, if you use a Python string and don't import System then the string doesn't support the usual .NET string methods such as ToUpper. That is:

c="Hello World"

will fail as a Python string object doesn't have a ToUpper Method.

However, if you try the same thing after importing System you will discover that it does work.

In other words, importing a .NET class can change the behavior of your program.

This is very odd and something you need to be aware, or perhaps beware, of.







Last Updated ( Wednesday, 21 July 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.