Information in this document is subject to change without notice. The example companies, organizations, products, people, and events depicted herein are fictitious. No association with any real company, organization, product, person or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

 

Microsoft may have patents, patent applications, trademarked, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

 

© Microsoft Corporation. All rights reserved.

 

Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual C#, Visual J#,  and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

 

Other product and company names herein may be the trademarks of their respective owners.


 

This source code is subject to terms and conditions of the Apache License, Version 2.0. A copy of the license can be found in the License.html file at the root of this distribution. If you cannot locate the Apache License, Version 2.0, please send an email to ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound by the terms of the Apache License, Version 2.0.

 

 

ClrType

 

 

Prerequisites

The prerequisites to successfully run this application are:

 

Getting Started

Please make sure the folder structure in the ClrType directory is:

.\sample.py
.\clrtype.py

HINT: if the folder containing the IronPython distribution is not already in your PATH environment variable, please add it from the command prompt you'll be working in.  By doing this you will be able to run the "ipy" command from any arbitrary directory:

set PATH=%PATH%;%ProgramFiles%\IronPython 2.6

From the command prompt, navigate to the ClrType directory and type the following:

ipy.exe sample.py

Overview

The ClrType sample shows how to define a class in IronPython with required CLR members. Normally, a class definition in Python does not map directly to a unique CLR type. This is because the semantics of classes is different between Python and the CLR. For example, in Python it is possible to change the base types just by assigning to the __bases__ attribute on the type object. However, the same is not possible with CLR classes. Hence, IronPython implements Python classes without mapping them directly to CLR types.

However, it is sometimes required to have control over the CLR type generated for the Python class. This is because some .NET APIs expect the user to define a CLR type with certain attributes and members. For example, to define a pinvoke method, the user is required to define a CLR type with a CLR method marked with DllImportAttribute, and where the signature of the CLR method exactly describes the target platform method. Similarly, Windows Communication Foundation (WCF) requires an interface to be marked with System.ServiceModel.ServiceContractAttribute to indicate that it defines a service contract.

Starting with IronPython 2.6, IronPython supports a low-level hook referred to as the __clrtype__ class attribute that allows customization of the CLR type corresponding to a Python class. However, this is a low-level hook, and the user is expected to build on top of it. The ClrType sample shows how to build on top of the __clrtype__ hook. The clrtype.ClrMetaclass metaclass can be used to define CLR fields, properties and class-level attributes. Also, you can declare declare strongly-typed methods.

In the future, the sample is expected to become a fully supported IronPython module.