Discussion:
[Python.NET] How to pass a Type to a method?
Daniel Krause
2013-01-24 20:11:29 UTC
Permalink
I would like to use System.Array.

The following code is running with iron python

import clr
import System
from System import Array
from System import Int32
from clr import GetClrType
rows = 1
columns = 2
#method: Array.CreateInstance(Type, Int32, Int32)
array = Array.CreateInstance(GetClrType(Int32), rows, columns)
print array.GetLength(0)
print array.GetLength(1)

ouptput:
1
2

The same code throws an import error in python for .NET
from clr import GetClrType
ImportError: cannot import name GetClrType

Is another syntax for python for .Net necessary?
Or is there perhaps another method to pass a Type to a method?
Tribble, Brett
2013-01-24 21:27:04 UTC
Permalink
I don't think the python.net clr module supports most of the methods etc. that the IronPython one does:

http://nullege.com/codes/search/clr

Can you use the following instead?

import System
...

...
array = Array.CreateInstance(System.Int32, rows, columns)

From: PythonDotNet [mailto:pythondotnet-bounces+btribble=ea.com-+ZN9ApsXKcFQFI55V6+***@public.gmane.orgg] On Behalf Of Daniel Krause
Sent: Thursday, January 24, 2013 12:11 PM
To: pythondotnet-+ZN9ApsXKcEdnm+***@public.gmane.org
Subject: [Python.NET] How to pass a Type to a method?

I would like to use System.Array.

The following code is running with iron python

import clr
import System
from System import Array
from System import Int32
from clr import GetClrType
rows = 1
columns = 2
#method: Array.CreateInstance(Type, Int32, Int32)
array = Array.CreateInstance(GetClrType(Int32), rows, columns)
print array.GetLength(0)
print array.GetLength(1)

ouptput:
1
2

The same code throws an import error in python for .NET
from clr import GetClrType
ImportError: cannot import name GetClrType

Is another syntax for python for .Net necessary?
Or is there perhaps another method to pass a Type to a method?
Daniel Krause
2013-01-24 21:36:32 UTC
Permalink
Wow, I tried so many different variants, but this I must have missed!

Thanks, it works!
I don’t think the python.net clr module supports most of the methods etc.
that the IronPython one does:****
** **
http://nullege.com/codes/search/clr****
** **
Can you use the following instead?****
** **
import System****
…****
** **
…****
array = Array.CreateInstance(System.Int32, rows, columns)****
** **
*From:* PythonDotNet [mailto:pythondotnet-bounces+btribble=
*Sent:* Thursday, January 24, 2013 12:11 PM
*Subject:* [Python.NET] How to pass a Type to a method?****
** **
I would like to use System.Array.****
** **
The following code is running with iron python****
** **
import clr****
import System****
from System import Array****
from System import Int32****
from clr import GetClrType****
rows = 1****
columns = 2****
#method: Array.CreateInstance(Type, Int32, Int32)****
array = Array.CreateInstance(GetClrType(Int32), rows, columns)****
print array.GetLength(0)****
print array.GetLength(1)****
** **
ouptput:****
1****
2****
** **
The same code throws an import error in python for .NET****
from clr import GetClrType****
ImportError: cannot import name GetClrType****
** **
Is another syntax for python for .Net necessary?****
Or is there perhaps another method to pass a Type to a method?****
Loading...