IteratorStateMachineAttribute Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Azt jelzi, hogy az Visual Basic metódusa Iterator módosítóval van-e megjelölve.
public ref class IteratorStateMachineAttribute sealed : System::Runtime::CompilerServices::StateMachineAttribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class IteratorStateMachineAttribute : System.Runtime.CompilerServices.StateMachineAttribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
[System.Serializable]
public sealed class IteratorStateMachineAttribute : System.Runtime.CompilerServices.StateMachineAttribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type IteratorStateMachineAttribute = class
inherit StateMachineAttribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
[<System.Serializable>]
type IteratorStateMachineAttribute = class
inherit StateMachineAttribute
Public NotInheritable Class IteratorStateMachineAttribute
Inherits StateMachineAttribute
- Öröklődés
- Attribútumok
Példák
Az alábbi példa bemutatja, hogyan állapítható meg, hogy egy metódus iterátormetódus-e. A példában IsIteratorMethod a következő lépéseket hajtja végre:
MethodInfo A metódusnév objektumának lekérése a következő használatávalType.GetMethod: .
A Type használatával beolvas egy objektumot az attribútumhoz.
A metódushoz és az attribútumtípushoz egy attribútumobjektumot kér le a használatával
MethodInfo.GetCustomAttribute. HaGetCustomAttributeNothing(Visual Basic) értéket ad vissza, a metódus nem tartalmazza az attribútumot.
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Threading.Tasks
Imports System.Runtime.CompilerServices
Module Module1
' This class is used by the code below to discover method attributes.
Public Class TheClass
Public Async Function AsyncMethod() As Task(Of Integer)
Await Task.Delay(5)
Return 1
End Function
Public Iterator Function IteratorMethod() As IEnumerable(Of Integer)
Yield 1
Yield 2
End Function
Public Function RegularMethod() As Integer
Return 0
End Function
End Class
Private Function IsAsyncMethod(classType As Type, methodName As String)
' Obtain the method with the specified name.
Dim method As MethodInfo = classType.GetMethod(methodName)
Dim attType As Type = GetType(AsyncStateMachineAttribute)
Dim attrib = CType(method.GetCustomAttribute(attType), AsyncStateMachineAttribute)
' The above variable contains the StateMachineType property.
Return (attrib IsNot Nothing)
End Function
Private Function IsIteratorMethod(classType As Type, methodName As String)
' Obtain the method with the specified name.
Dim method As MethodInfo = classType.GetMethod(methodName)
Dim attType As Type = GetType(IteratorStateMachineAttribute)
' Obtain the custom attribute for the method.
' The value returned contains the StateMachineType property.
' Nothing is returned if the attribute isn't present for the method.
Dim attrib = CType(method.GetCustomAttribute(attType), IteratorStateMachineAttribute)
Return (attrib IsNot Nothing)
End Function
Private Sub ShowResult(classType As Type, methodName As String)
Console.Write((methodName & ": ").PadRight(16))
If IsAsyncMethod(classType, methodName) Then
Console.WriteLine("Async method")
ElseIf IsIteratorMethod(classType, methodName) Then
Console.WriteLine("Iterator method")
Else
Console.WriteLine("Regular method")
End If
' Note: The IteratorStateMachineAttribute applies to Visual Basic methods
' but not C# methods.
End Sub
Sub Main()
ShowResult(GetType(TheClass), "AsyncMethod")
ShowResult(GetType(TheClass), "IteratorMethod")
ShowResult(GetType(TheClass), "RegularMethod")
Console.ReadKey()
End Sub
' AsyncMethod: Async method
' IteratorMethod: Iterator method
' RegularMethod: Regular method
End Module
Megjegyzések
Ne alkalmazza az attribútumot a IteratorStateMachine kód metódusaira. A Iterator módosítóval rendelkező Visual Basic metódusok esetében a fordító a IteratorStateMachine attribútumot alkalmazza az általa kibocsátott IL-ben.
Ha egy metódus (MethodName) Visual Basic rendelkezik a Iterator módosítóval, a fordító egy állapotgép-struktúrát tartalmazó IL-t bocsát ki. Ez a struktúra tartalmazza a metódus kódját. Ez az IL tartalmaz egy csonkmetódust (MethodName), amely meghívja az állapotgépet. A fordító hozzáadja az attribútumot a IteratorStateMachine csonk metódushoz, hogy az eszközök azonosítani tudják a megfelelő állapotgépet. A kibocsátott IL részletei változhatnak a fordítók későbbi kiadásaiban.
Az iterátor metódus egyéni iterációt hajt végre egy gyűjteményen keresztül a Yield (Visual Basic) utasítással, hogy egyenként adja vissza az egyes elemeket. További információ: Iterators.
Note
Nem IteratorStateMachineAttribute tesztelheti, hogy egy metódus iterátormetódus-e a C#-ban.
Konstruktorok
| Name | Description |
|---|---|
| IteratorStateMachineAttribute(Type) |
Inicializálja a IteratorStateMachineAttribute osztály új példányát. |
Tulajdonságok
| Name | Description |
|---|---|
| StateMachineType |
Az állapotgép-metódus implementálásához a fordító által létrehozott mögöttes állapotgép-típus típusobjektumát adja vissza. (Öröklődés forrása StateMachineAttribute) |
| TypeId |
Ha származtatott osztályban implementálják, ehhez egy egyedi azonosítót Attributekap. (Öröklődés forrása Attribute) |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Olyan értéket ad vissza, amely jelzi, hogy ez a példány egyenlő-e egy adott objektummal. (Öröklődés forrása Attribute) |
| GetHashCode() |
A példány kivonatkódját adja vissza. (Öröklődés forrása Attribute) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| IsDefaultAttribute() |
Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy a példány értéke-e a származtatott osztály alapértelmezett értéke. (Öröklődés forrása Attribute) |
| Match(Object) |
Származtatott osztály felülírásakor egy olyan értéket ad vissza, amely jelzi, hogy ez a példány egy adott objektummal egyenlő-e. (Öröklődés forrása Attribute) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |
Explicit interfész-implementációk
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Névkészletet képez le a küldési azonosítók megfelelő készletére. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Lekéri egy objektum típusadatait, amelyek a felület típusadatainak lekérésére használhatók. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Lekéri az objektumok által biztosított típusinformációs felületek számát (0 vagy 1). (Öröklődés forrása Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Hozzáférést biztosít az objektumok által közzétett tulajdonságokhoz és metódusokhoz. (Öröklődés forrása Attribute) |