Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Can a multidimensional array be marshaled into managed code?


Asked by Lennox Guerra on Dec 08, 2021 FAQ



Multidimensional, or nonzero-bound safe arrays, can be marshaled into managed code if the method signature produced by Tlbimp.exe is modified to indicate an element type of ELEMENT_TYPE_ARRAY instead of ELEMENT_TYPE_SZARRAY. Alternatively, you can use the /sysarray switch with Tlbimp.exe to import all arrays as System.Array objects.
Indeed,
When marshaling arrays from unmanaged code to managed code, the marshaler checks the MarshalAsAttribute associated with the parameter to determine the array size. If the array size is not specified, only one element is marshaled. The MarshalAsAttribute has no effect on marshaling managed arrays to unmanaged code.
Subsequently, Default Marshaling for Arrays. In an application consisting entirely of managed code, the common language runtime passes array types as In/Out parameters. In contrast, the interop marshaler passes an array as In parameters by default.
Accordingly,
An array is a reference type in managed code that contains one or more elements of the same type. Although arrays are reference types, they are passed as In parameters to unmanaged functions. This behavior is inconsistent with way managed arrays are passed to managed objects, which is as In/Out parameters.
Moreover,
Array of structures with integers. Array of structures with strings. Unless an array is explicitly marshaled by reference, the default behavior marshals the array as an In parameter. You can change this behavior by applying the InAttribute and OutAttribute attributes explicitly.