Type.IsExplicitLayout Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar ett värde som anger om fälten av den aktuella typen anges vid uttryckligen angivna förskjutningar.
public:
property bool IsExplicitLayout { bool get(); };
public bool IsExplicitLayout { get; }
member this.IsExplicitLayout : bool
Public ReadOnly Property IsExplicitLayout As Boolean
Egenskapsvärde
true om egenskapen Attributes för den aktuella typen innehåller ExplicitLayout, annars , false.
Implementeringar
Exempel
I följande exempel skapas en instans av en typ och värdet för dess IsExplicitLayout egenskap visas. Den använder MySystemTime klassen, som också finns i kodexemplet för StructLayoutAttribute.
using System;
using System.Reflection;
using System.ComponentModel;
using System.Runtime.InteropServices;
// Class to test for the ExplicitLayout property.
[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
public class MySystemTime
{
[FieldOffset(0)]public ushort wYear;
[FieldOffset(2)]public ushort wMonth;
[FieldOffset(4)]public ushort wDayOfWeek;
[FieldOffset(6)]public ushort wDay;
[FieldOffset(8)]public ushort wHour;
[FieldOffset(10)]public ushort wMinute;
[FieldOffset(12)]public ushort wSecond;
[FieldOffset(14)]public ushort wMilliseconds;
}
public class Program
{
public static void Main(string[] args)
{
// Create an instance of the type using the GetType method.
Type t = typeof(MySystemTime);
// Get and display the IsExplicitLayout property.
Console.WriteLine("\nIsExplicitLayout for MySystemTime is {0}.",
t.IsExplicitLayout);
}
}
open System.Runtime.InteropServices
// Class to test for the ExplicitLayout property.
[<StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)>]
type MySystemTime =
[<FieldOffset 0>] val public wYear: uint16
[<FieldOffset 2>] val public wMonth: uint16
[<FieldOffset 4>] val public wDayOfWeek: uint16
[<FieldOffset 6>] val public wDay: uint16
[<FieldOffset 8>] val public wHour: uint16
[<FieldOffset 10>] val public wMinute: uint16
[<FieldOffset 12>] val public wSecond: uint16
[<FieldOffset 14>] val public wMilliseconds: uint16
// Create an instance of the type using the GetType method.
let t = typeof<MySystemTime>
// Get and display the IsExplicitLayout property.
printfn $"\nIsExplicitLayout for MySystemTime is {t.IsExplicitLayout}."
Imports System.Reflection
Imports System.ComponentModel
Imports System.Runtime.InteropServices
'Class to test for the ExplicitLayout property.
<StructLayout(LayoutKind.Explicit, Size := 16, CharSet := CharSet.Ansi)> _
Public Class MySystemTime
<FieldOffset(0)> Public wYear As Short
<FieldOffset(2)> Public wMonth As Short
<FieldOffset(4)> Public wDayOfWeek As Short
<FieldOffset(6)> Public wDay As Short
<FieldOffset(8)> Public wHour As Short
<FieldOffset(10)> Public wMinute As Short
<FieldOffset(12)> Public wSecond As Short
<FieldOffset(14)> Public wMilliseconds As Short
End Class
Public Class Program
Public Shared Sub Main()
'Create an instance of type using the GetType method.
Dim t As Type = GetType(MySystemTime)
' Get and display the IsExplicitLayout property.
Console.WriteLine(vbCrLf & "IsExplicitLayout for MySystemTime is {0}.", _
t.IsExplicitLayout)
End Sub
End Class
Kommentarer
Den här egenskapen tillhandahålls som en bekvämlighet. Du kan också använda TypeAttributes.LayoutMask uppräkningsvärdet för att välja typlayoutattributen och sedan testa om TypeAttributes.ExplicitLayout har angetts. Uppräkningsvärdena TypeAttributes.AutoLayout, TypeAttributes.ExplicitLayoutoch TypeAttributes.SequentialLayout anger hur fälten för typen anges i minnet.
För dynamiska typer kan du ange TypeAttributes.ExplicitLayout när du skapar typen. I kod använder du StructLayoutAttribute attributet med LayoutKind.Explicit uppräkningsvärdet för typen för att ange att de förskjutningar som fälten börjar med uttryckligen anges.
Note
Du kan inte använda GetCustomAttributes metoden för att avgöra om har StructLayoutAttribute tillämpats på en typ.
Om strömmen Type representerar en konstruerad allmän typ gäller den här egenskapen för den generiska typdefinition som typen skapades från. Om den aktuella Type till exempel representerar MyGenericType<int> (MyGenericType(Of Integer) i Visual Basic) bestäms värdet för den här egenskapen av MyGenericType<T>.
Om strömmen Type representerar en typparameter i definitionen av en allmän typ eller allmän metod returnerar falseden här egenskapen alltid .