MarshalAsAttribute.SizeParamIndex 欄位

定義

指示哪一個以零起始的參數含有陣列元素的計數,類似 COM 中的 size_is

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

欄位值

範例

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

備註

欄位 SizeParamIndex 支援 Managed 到 Unmanaged 和 Unmanaged-to-managed 呼叫。 它不會影響呼叫 COM 物件的 Managed 程式碼。

根據 Managed 型別和套用至它的屬性,陣列可以傳遞為安全陣列或 C 樣式陣列。

當陣列以 C 樣式陣列傳遞時,封送處理器無法判斷陣列的大小。 因此,若要將 Managed 陣列傳遞至 Unmanaged 函式或方法,您必須提供兩個引數:

  • 由參考或值定義的陣列。

  • 陣列大小,由參考或值定義。

陣列大小參數以零起始的索引是使用 SizeParamIndex 欄位所定義。

如果您同時 SizeParamIndex 使用 欄位來指定 和 UnmanagedType.LPArrayMarshalAsAttribute.SizeConst ,則欄位值的總和會產生大小總計。

如需詳細資訊,請參閱 陣列的預設封送處理

適用於

另請參閱