MarshalAsAttribute.SizeParamIndex 필드

정의

COM에 있는 size_is와 같이 배열 요소의 수가 들어 있고 0부터 시작하는 매개 변수를 나타냅니다.

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 필드는 관리형에서 관리되지 않는 호출 및 관리되지 않는 호출을 지원합니다. COM 개체를 호출하는 관리 코드에는 영향을 주지 않습니다.

관리되는 형식 및 적용된 특성에 따라 배열을 안전한 배열 또는 C 스타일 배열로 전달할 수 있습니다.

배열이 C 스타일 배열로 전달되면 마샬러는 배열의 크기를 확인할 수 없습니다. 따라서 관리되는 배열을 관리되지 않는 함수 또는 메서드에 전달하려면 다음 두 개의 인수를 제공해야 합니다.

  • 참조 또는 값으로 정의된 배열입니다.

  • 참조 또는 값으로 정의된 배열 크기입니다.

배열 크기 매개 변수의 인덱스(0부터 시작)는 필드를 사용하여 정의됩니다 SizeParamIndex .

필드로 및 MarshalAsAttribute.SizeConstUnmanagedType.LPArray 모두 SizeParamIndex 지정하면 필드 값의 합계가 총 크기가 생성됩니다.

자세한 내용은 배열에 대한 기본 마샬링을 참조하세요.

적용 대상

추가 정보