Array.CreateInstance(<type>, System.Int32[]) results in 'TypeError: No method matches given arguments' in v2.5.0
See original GitHub issueEnvironment
- Pythonnet version: 2.5.0
- Python version: 3.6.9
- Operating System: Windows 10
Details
- Describe what you were trying to get done.
Array.CreateInstance for multi-dimensional arrays worked fine in v2.4.0. After upgrading to v2.5.0+, an error message gets thrown.
- What commands did you run to trigger this issue?
import numpy as np
import ctypes
import clr, System
from System import Array, Int32
from System.Runtime.InteropServices import GCHandle, GCHandleType
import sys
import os
_MAP_NP_NET = {
np.dtype('float32'): System.Single,
np.dtype('float64'): System.Double,
np.dtype('int8') : System.SByte,
np.dtype('int16') : System.Int16,
np.dtype('int32') : System.Int32,
np.dtype('int64') : System.Int64,
np.dtype('uint8') : System.Byte,
np.dtype('uint16') : System.UInt16,
np.dtype('uint32') : System.UInt32,
np.dtype('uint64') : System.UInt64,
np.dtype('bool') : System.Boolean,
}
def asNetArray(npArray):
dims = npArray.shape
dtype = npArray.dtype
netDims = Array.CreateInstance(Int32, npArray.ndim)
for I in range(npArray.ndim):
netDims[I] = int(dims[I])
if not npArray.flags.c_contiguous:
npArray = npArray.copy(order='C')
assert npArray.flags.c_contiguous
try:
netArray = Array.CreateInstance(_MAP_NP_NET[dtype], netDims)
except KeyError:
raise NotImplementedError("asNetArray does not yet support dtype {}".format(dtype))
try: # Memmove
destHandle = GCHandle.Alloc(netArray, GCHandleType.Pinned)
sourcePtr = npArray.__array_interface__['data'][0]
destPtr = destHandle.AddrOfPinnedObject().ToInt64()
ctypes.memmove(destPtr, sourcePtr, npArray.nbytes)
finally:
if destHandle.IsAllocated: destHandle.Free()
return netArray
if __name__ == '__main__':
foo = np.full([1024,1024], 2.5, dtype=np.int32)
netFoo = asNetArray( foo )
- If there was a crash, please include the traceback here.
Traceback (most recent call last):
File "pythonnetUtils.py", line 53, in <module>
netFoo = asNetArray( foo )
File "pythonnetUtils.py", line 37, in asNetArray
netArray = Array.CreateInstance(_MAP_NP_NET[dtype], netDims)
TypeError: No method matches given arguments for CreateInstance: (<class 'CLR.CLR Metatype'>, <class 'System.Int32[]'>)
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
python.NET upgrade to python 3.7: IntPtr to numpy array ...
I'm aware that there were some changes to python types from 2 to 3 but searching and playing around with the constructor didn't...
Read more >Array.CreateInstance Method (System)
CreateInstance (Type, Int32) Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
Read more >Python access keywords for using compiled .NET method
Hi, I have a method like this in c# bool method(ref double[] values){ values = new ... TypeError: No method matches given arguments...
Read more >[Python.NET] Re: "TypeError: No method matches given...
The following code works: from System import UInt32, Int32 from System import Double result, response, errString = ESP301Device.
Read more >IronPython .NET Integration
Int32, System.Boolean) >>> ba = BitArray(ba) # calls __new__(System.Collections.BitArray). The argument types do not have be an exact match with the method ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Running through the changes between v2.4.0 and v2.5.0, the last commit that works for the above test is (4a92d80)
Okay, then it’s indeed https://github.com/pythonnet/pythonnet/pull/1106. That change actually improved things, in that it allows “proper” use of
paramsarrays (likeCreateInstanceuses). You can fix your code by changingto
I’m not yet completely sure whether we want to fix it (we definitely want to keep the feature, maybe we can still allow the direct passing of the array as a fallback), but I acknowledge breakage 😃
/edit: Actually you can even drop the whole copy-to-.net part and replace it by