MarshalAsAttribute.SizeParamIndex Campo

Definizione

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

C#
public short SizeParamIndex;

Valore del campo

Esempio

C#
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();
    }
}

Commenti

Il SizeParamIndex campo supporta chiamate gestite da non gestite e non gestite. 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 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.

  • Dimensioni della matrice definite da riferimento o valore.

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

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

Per altre informazioni, vedere Marshalling predefinito per le matrici.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche