MarshalAsAttribute.SizeParamIndex Alan

Tanım

DIZI öğelerinin sayısını içeren, COM'dakine benzer sıfır tabanlı parametreyi size_is gösterir.

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

Alan Değeri

Örnekler

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

Açıklamalar

alanı SizeParamIndex yönetilenden yönetilmeyene ve yönetilmeyenden yönetilene çağrıları destekler. COM nesnelerini çağıran yönetilen kod üzerinde herhangi bir etkisi yoktur.

Yönetilen türe ve bu türe uygulanan özniteliklere bağlı olarak, dizi güvenli bir dizi veya C stili dizi olarak geçirilebilir.

Diziler C stili diziler olarak geçirildiğinde, sıralayıcı dizinin boyutunu belirleyemez. Bu nedenle, yönetilen bir diziyi yönetilmeyen bir işleve veya yönteme geçirmek için iki bağımsız değişken sağlamanız gerekir:

  • Başvuru veya değerle tanımlanan dizi.

  • Başvuru veya değerle tanımlanan dizi boyutu.

Dizi boyutu parametresinin sıfır tabanlı dizini, alanı kullanılarak SizeParamIndex tanımlanır.

Hem hem de SizeParamIndexMarshalAsAttribute.SizeConst alanını UnmanagedType.LPArray belirtirseniz, alanların değerlerinin toplamı bir boyut toplamı oluşturur.

Daha fazla bilgi için bkz . Diziler için Varsayılan Hazırlama.

Şunlara uygulanır

Ayrıca bkz.