MarshalAsAttribute.SizeParamIndex Campo

Definizione

Indica il parametro in base zero che contiene il numero di elementi della matrice, simile a size_is in COM.

public: short SizeParamIndex;
public short SizeParamIndex;
val mutable SizeParamIndex : int16
Public SizeParamIndex As Short 

Valore del campo

Esempio

using namespace System;
using namespace System::Runtime::InteropServices;

// Force the layout of your fields to the C-style struct layout.
// Without this, the .NET Framework will reorder your fields.

[StructLayoutAttribute(LayoutKind::Sequential)]
value struct Vertex
{
public:
   float x;
   float y;
   float z;
};


// Add [In] or [In, Out] attributes as appropriate.
// Marshal as a C-style array of Vertex, where the second (SizeParamIndex is zero-based)
// parameter (size) contains the count of array elements.

[DllImport("SomeDLL.dll")]
extern void SomeUnsafeMethod( [MarshalAs(UnmanagedType::LPArray,SizeParamIndex=1)]array<Vertex>^data, long size );
int main()
{
   array<Vertex>^verts = gcnew array<Vertex>(3);
   SomeUnsafeMethod( verts, verts->Length );
}
using System.Runtime.InteropServices;
using SomeNamespace;

namespace SomeNamespace
{
    // Force the layout of your fields to the C style struct layout.
    // Without this, the .NET Framework will reorder your fields.
    [StructLayout(LayoutKind.Sequential)]
    public struct Vertex
    {
        float	x;
    float	y;
        float	z;
    }

    class SomeClass
    {
        // Add [In] or [In, Out] attributes as approppriate.
        // Marshal as a C style array of Vertex, where the second (SizeParamIndex is zero-based)
        //  parameter (size) contains the count of array elements.
        [DllImport ("SomeDll.dll")]
        public static extern void SomeUnsafeMethod(
                                      [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] Vertex[] data,
                                      long size );

        public void SomeMethod()
        {
            Vertex[] verts = new Vertex[3];
            SomeUnsafeMethod( verts, verts.Length );
        }
    }
}

class Test
{
    public static void Main()
    {
        SomeClass AClass = new SomeClass();

        AClass.SomeMethod();
    }
}
Option Strict Off

Imports System.Runtime.InteropServices
Imports SomeNamespace

Namespace SomeNamespace
    ' Force the layout of your fields to the C style struct layout.
    ' Without this, the .NET Framework will reorder your fields.
    
    <StructLayout(LayoutKind.Sequential)> _
    Structure Vertex
        Dim x As Decimal
        Dim y As Decimal
        Dim z As Decimal
    End Structure

    Class SomeClass
        ' Add [In] or [In, Out] attributes as approppriate.
        ' Marshal as a C style array of Vertex, where the second (SizeParamIndex is zero-based)
        '  parameter (size) contains the count of array elements.

        Declare Auto Sub SomeUnsafeMethod Lib "somelib.dll" ( _
                                      <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=1)> data() As Vertex, _
                                      size As Long ) 

        Public Sub SomeMethod()
            Dim verts(3) As Vertex
            SomeUnsafeMethod( verts, verts.Length )
        End Sub

    End Class

End Namespace

Module Test
    Sub Main
        Dim AClass As New SomeClass

        AClass.SomeMethod
        End Sub
End Module

Commenti

Il SizeParamIndex campo supporta chiamate gestite da gestito a non gestito e non gestito da a gestito. Non ha alcun effetto sul codice gestito che chiama oggetti COM.

A seconda del tipo gestito e degli attributi applicati, la matrice può essere passata come matrice sicura o matrice in stile C.

Quando le matrici vengono passate come matrici in stile C, il gestore di marshalling non può determinare le dimensioni della matrice. Pertanto, per passare una matrice gestita a una funzione o a un metodo non gestito, è necessario specificare due argomenti:

  • Matrice, definita da riferimento o valore.

  • Dimensione della matrice, definita dal riferimento o dal valore.

L'indice in base zero del parametro delle dimensioni della matrice viene definito usando il SizeParamIndex campo .

Se si specificano sia SizeParamIndex che MarshalAsAttribute.SizeConst con un UnmanagedType.LPArray campo, la somma dei valori dei campi produce un totale di dimensioni.

Per altre informazioni, vedere Marshalling predefinito per matrici.

Si applica a

Vedi anche