MarshalAsAttribute.SizeParamIndex Veld
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Geeft de op nul gebaseerde parameter aan die het aantal matrixelementen bevat, vergelijkbaar met size_is in COM.
public: short SizeParamIndex;
public short SizeParamIndex;
val mutable SizeParamIndex : int16
Public SizeParamIndex As Short
Waarde van veld
Voorbeelden
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
Opmerkingen
Het SizeParamIndex veld ondersteunt beheerde naar onbeheerde en niet-beheerde aanroepen. Het heeft geen effect op beheerde code die COM-objecten aanroept.
Afhankelijk van het beheerde type en de kenmerken die erop zijn toegepast, kan de matrix worden doorgegeven als een veilige matrix of C-stijlmatrix.
Wanneer matrices worden doorgegeven als C-achtige matrices, kan de marshaler de grootte van de matrix niet bepalen. Als u een beheerde matrix wilt doorgeven aan een niet-beheerde functie of methode, moet u daarom twee argumenten opgeven:
De matrix, gedefinieerd met verwijzing of waarde.
De matrixgrootte, gedefinieerd met verwijzing of waarde.
De op nul gebaseerde index van de parameter matrixgrootte wordt gedefinieerd met behulp van het SizeParamIndex veld.
Als u zowel SizeParamIndex als MarshalAsAttribute.SizeConst met een UnmanagedType.LPArray veld opgeeft, produceert de som van de waarden van de velden een groottetotaal.
Zie Default Marshaling voor matrices voor meer informatie.