jclement.ca
3Nov/030

Python and .NET (Part II)

So I was playing a bit more with the Python.NET beta and it's very cool. I wrote the following class library in C# and compiled it to testlib.dll.

using System;
namespace testlib
{
	public class TestClass
	{
		private int mNum;
		public TestClass()
		{
			mNum = 1;
		}
		public void inc()
		{
			mNum += 1;
		}
		public void PrintNum()
		{
			System.Console.WriteLine(mNum);
		}
		public static void StaticTest()
		{
			System.Console.WriteLine("Hello World");
		}
	}
}

Now if I copy the testlib.dll to the Python.NET directory I can do the following:

>>> from CLR.testlib import TestClass
>>> c = TestClass()
>>> c.PrintNum()
1
>>> for i in range(100): c.inc()
>>> c.PrintNum()
101
>>> c.StaticTest()
Hello World
>>> TestClass.StaticTest()
Hello World
>>> dir(c)
['Equals', 'Finalize', 'GetHashCode', 'GetType', 'MemberwiseClone',
'PrintNum','ReferenceEquals', 'StaticTest', 'ToString', '__call__',
'__class__', '__cmp__','__delattr__', '__delitem__', '__doc__',
'__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__setitem__', '__str__', 'inc']

Very cool! Again this seems like a really handy way to test .NET assemblies by poking and proding them at runtime.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.