Python is a high level, object oriented and portable language. The dotnet4py library is an extension to Python which allows Python to interact with the .NET runtime. Using this library, one can, from Python:
All of this is done using the Python syntax which is very
similar to that of C# or Java, but with a few differences.
Here’s a simple example where we will invoke an API on a .NET class:
C:\>python Python 2.3b1 (#40,
Type
"help", "copyright", "credits" or
"license" for more information. >>>
import dotnet4pylib dotnet4py - A
Python Wrapper for .NET Copyright (C) 2003
All Rights
Reserved Warning!!!
Environment variable DOTNET4PY_ASSEMBLY_PATH not set. Setting default to '.' >>> |
Step 1: We import the dotnet4pylib module
Python 2.3.4 (#53, Type "help", "copyright",
"credits" or "license" for more information. >>> import dotnet4pylib dotnet4py - A Python Wrapper for .NET Copyright (C) 2003 All Rights Reserved >>> from CLR.System.Xml.dll.System.Xml
import XmlDocument >>> doc = XmlDocument() >>> doc.LoadXml("<tag>contents</tag>") >>> |
Step 2. Import the target class from the desired assembly and instantiate an object
>>>
dir(doc) ['.ctor', 'AppendChild',
'Attributes', 'BaseURI', 'ChildNodes',
'Clone', 'CloneN ode', 'CreateAttribute', 'CreateCDataSection',
'CreateComment', 'CreateDocumentF ragment', 'CreateDocumentType',
'CreateElement', 'CreateEntityReference',
'Creat eNavigator', 'CreateNode',
'CreateProcessingInstruction', 'CreateSignificantWhit espace', 'CreateTextNode',
'CreateWhitespace', 'CreateXmlDeclaration',
'Document Element', 'DocumentType', 'Equals', 'FirstChild',
'GetElementById', 'GetElements ByTagName', 'GetEnumerator',
'GetHashCode', 'GetNamespaceOfPrefix',
'GetPrefixOf Namespace', 'GetType', 'HasChildNodes',
'Implementation', 'ImportNode', 'InnerTe xt', 'InnerXml', 'InsertAfter', 'InsertBefore', 'IsReadOnly', 'Item', 'LastChild ', 'Load', 'LoadXml', 'LocalName', 'Name',
'NameTable', 'NamespaceURI',
'NextSib ling', 'NodeChanged', 'NodeChanging', 'NodeInserted', 'NodeInserting',
'NodeRemo ved', 'NodeRemoving',
'NodeType', 'Normalize', 'OuterXml',
'OwnerDocument', 'Par entNode', 'Prefix', 'PrependChild',
'PreserveWhitespace', 'PreviousSibling',
'Re adNode', 'RemoveAll', 'RemoveChild', 'ReplaceChild',
'Save', 'SelectNodes', 'Sel ectSingleNode', 'Supports', 'ToString',
'Value', 'WriteContentTo', 'WriteTo',
'X mlResolver', '__doc__', '__getitem__',
'__init__', '__len__', '__module__', 'add _NodeChanged', 'add_NodeChanging',
'add_NodeInserted', 'add_NodeInserting',
'add _NodeRemoved', 'add_NodeRemoving',
'dotnet4pyObj', 'fillMyDict', 'get_Attributes ', 'get_BaseURI', 'get_ChildNodes',
'get_DocumentElement', 'get_DocumentType',
' get_FirstChild', 'get_HasChildNodes',
'get_Implementation', 'get_InnerText',
'ge t_InnerXml', 'get_IsReadOnly',
'get_Item', 'get_LastChild',
'get_LocalName', 'ge t_Name', 'get_NameTable',
'get_NamespaceURI', 'get_NextSibling',
'get_NodeType', 'get_OuterXml', 'get_OwnerDocument', 'get_ParentNode',
'get_Prefix', 'get_Prese rveWhitespace', 'get_PreviousSibling',
'get_Value', 'remove_NodeChanged',
'remov e_NodeChanging', 'remove_NodeInserted',
'remove_NodeInserting', 'remove_NodeRemo ved', 'remove_NodeRemoving',
'set_InnerText', 'set_InnerXml',
'set_Prefix', 'set _PreserveWhitespace', 'set_Value',
'set_XmlResolver'] >>> |
Step 3 Inspect available .NET methods/properties/attributes