Skip to content

Array.CreateInstance(<type>, System.Int32[]) results in 'TypeError: No method matches given arguments' in v2.5.0 #1187

@pkthong

Description

@pkthong

Environment

  • 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[]'>)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions