IteratorStateMachineAttribute Klasse
Definition
Gibt an, ob eine Methode in Visual Basic mit dem Iterator
markiert ist.Indicates whether a method in Visual Basic is marked with the Iterator
modifier.
public ref class IteratorStateMachineAttribute sealed : System::Runtime::CompilerServices::StateMachineAttribute
public sealed class IteratorStateMachineAttribute : 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
type IteratorStateMachineAttribute = class
inherit 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
- Vererbung
- Attribute
Beispiele
Im folgenden Beispiel wird gezeigt, wie Sie bestimmen können, ob eine Methode eine Iteratormethode ist.The following example shows how to determine whether a method is an iterator method. Im Beispiel IsIteratorMethod
führt die folgenden Schritte aus:In the example, IsIteratorMethod
performs the following steps:
Ruft ein- MethodInfo Objekt für den Methodennamen mithilfe von ab Type.GetMethod .Obtains a MethodInfo object for the method name by using Type.GetMethod.
Ruft ein- Type Objekt für das-Attribut mit dem GetType-Operatorab.Obtains a Type object for the attribute by using GetType Operator.
Ruft mithilfe von ein Attribut Objekt für die Methode und den Attributtyp ab
MethodInfo.GetCustomAttribute
.Obtains an attribute object for the method and attribute type by usingMethodInfo.GetCustomAttribute
. WennGetCustomAttribute
Nothing
(Visual Basic) zurückgibt, enthält die-Methode das-Attribut nicht.IfGetCustomAttribute
returnsNothing
(Visual Basic), the method doesn't contain the attribute.
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
Hinweise
Sie sollten das- IteratorStateMachine
Attribut nicht auf Methoden in Ihrem Code anwenden.You shouldn't apply the IteratorStateMachine
attribute to methods in your code. Für Methoden in Visual Basic, die über den iteratormodifizierer verfügen, wendet der Compiler das- IteratorStateMachine
Attribut in der von ihm ausgegebenen Il an.For methods in Visual Basic that have the Iterator modifier, the compiler will apply the IteratorStateMachine
attribute in the IL that it emits.
Wenn eine Methode ( MethodName ) in Visual Basic über den- Iterator
Modifizierer verfügt, gibt der Compiler Il aus, die eine Zustands Automaten Struktur enthält.When a method ( MethodName ) in Visual Basic has the Iterator
modifier, the compiler emits IL that includes a state machine structure. Diese Struktur enthält den Code in der-Methode.That structure contains the code in the method. Diese Il enthält auch eine Stub-Methode ( MethodName ), die den Zustands Automat aufruft.That IL also contains a stub method ( MethodName ) that calls into the state machine. Der Compiler fügt der IteratorStateMachine
Stub-Methode das-Attribut hinzu, damit Tools den entsprechenden Zustands Automat identifizieren können.The compiler adds the IteratorStateMachine
attribute to the stub method so that tools can identify the corresponding state machine. Details der ausgegebenen Il können sich in zukünftigen Versionen der Compiler ändern.Details of the emitted IL might change in future releases of the compilers.
Eine Iteratormethode führt eine benutzerdefinierte iterierung für eine Auflistung durch, indem die Yield
(Visual Basic)-Anweisung verwendet wird, um jedes Element einzeln zurückzugeben.An iterator method performs a custom iteration over a collection by using the Yield
(Visual Basic) statement to return each element one at a time. Weitere Informationen finden Sie unter Iteratoren.For more information, see Iterators.
Hinweis
Sie können nicht verwenden IteratorStateMachineAttribute
, um zu testen, ob eine Methode eine Iteratormethode in c# ist.You can't use IteratorStateMachineAttribute
to test whether a method is an iterator method in C#.
Konstruktoren
IteratorStateMachineAttribute(Type) |
Initialisiert eine neue Instanz der IteratorStateMachineAttribute-Klasse.Initializes a new instance of the IteratorStateMachineAttribute class. |
Eigenschaften
StateMachineType |
Gibt das Typobjekt für den zugrunde liegenden Zustandsautomaten zurück, der vom Compiler generiert wurde, um die Zustandsautomat-Methode zu implementieren.Returns the type object for the underlying state machine type that was generated by the compiler to implement the state machine method. (Geerbt von StateMachineAttribute) |
TypeId |
Ruft bei Implementierung in einer abgeleiteten Klasse einen eindeutigen Bezeichner für dieses Attribute ab.When implemented in a derived class, gets a unique identifier for this Attribute. (Geerbt von Attribute) |
Methoden
Equals(Object) |
Gibt einen Wert zurück, der angibt, ob diese Instanz gleich einem angegebenen Objekt ist.Returns a value that indicates whether this instance is equal to a specified object. (Geerbt von Attribute) |
GetHashCode() |
Gibt den Hashcode für diese Instanz zurück.Returns the hash code for this instance. (Geerbt von Attribute) |
GetType() |
Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance. (Geerbt von Object) |
IsDefaultAttribute() |
Gibt beim Überschreiben in einer abgeleiteten Klasse an, ob der Wert der Instanz der Standardwert für die abgeleitete Klasse ist.When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Geerbt von Attribute) |
Match(Object) |
Beim Überschreiben in einer abgeleiteten Klasse wird ein Wert zurückgegeben, der angibt, ob diese Instanz einem bestimmten Objekt entspricht.When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Geerbt von Attribute) |
MemberwiseClone() |
Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object. (Geerbt von Object) |
ToString() |
Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object. (Geerbt von Object) |
Explizite Schnittstellenimplementierungen
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Ordnet eine Reihe von Namen einer entsprechenden Reihe von Dispatchbezeichnern zu.Maps a set of names to a corresponding set of dispatch identifiers. (Geerbt von Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Ruft die Typinformationen für ein Objekt ab, mit deren Hilfe die Typinformationen für eine Schnittstelle abgerufen werden können.Retrieves the type information for an object, which can be used to get the type information for an interface. (Geerbt von Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
Ruft die Anzahl der Schnittstellen mit Typinformationen ab, die von einem Objekt bereitgestellt werden (0 oder 1).Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Geerbt von Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Stellt den Zugriff auf von einem Objekt verfügbar gemachte Eigenschaften und Methoden bereit.Provides access to properties and methods exposed by an object. (Geerbt von Attribute) |