Share via


MarshalAsAttribute.SizeParamIndex Campo

Definição

Indica o parâmetro baseado em zero que contém a contagem de elementos da matriz, semelhante a size_is em COM.

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

Valor do campo

Exemplos

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

Comentários

O SizeParamIndex campo dá suporte a chamadas gerenciadas para não gerenciadas e não gerenciadas para gerenciadas. Ele não tem nenhum efeito sobre o código gerenciado que chama objetos COM.

Dependendo do tipo gerenciado e dos atributos aplicados a ele, a matriz pode ser passada como uma matriz segura ou matriz de estilo C.

Quando as matrizes são passadas como matrizes de estilo C, o marshaler não pode determinar o tamanho da matriz. Portanto, para passar uma matriz gerenciada para uma função ou método não gerenciado, você deve fornecer dois argumentos:

  • A matriz, definida por referência ou valor.

  • O tamanho da matriz, definido por referência ou valor.

O índice baseado em zero do parâmetro de tamanho da matriz é definido usando o SizeParamIndex campo .

Se você especificar e SizeParamIndexMarshalAsAttribute.SizeConst com um UnmanagedType.LPArray campo , a soma dos valores dos campos produzirá um total de tamanho.

Para obter mais informações, consulte Marshaling padrão para matrizes.

Aplica-se a

Confira também