IteratorStateMachineAttribute 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示是否使用 Iterator 修饰符标记 Visual Basic 中的方法。
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 StateMachineAttributePublic NotInheritable Class IteratorStateMachineAttribute
Inherits StateMachineAttribute- 继承
- 属性
示例
以下示例演示如何确定方法是否为迭代器方法。 在示例中, IsIteratorMethod 执行以下步骤:
- MethodInfo使用 Type.GetMethod获取方法名称的 对象。 
- Type使用 GetType 运算符获取 属性的 对象。 
- 使用 - MethodInfo.GetCustomAttribute获取方法和特性类型的属性对象。 如果- GetCustomAttribute返回- Nothing(Visual Basic) ,则该方法不包含 属性。
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
注解
不应将 IteratorStateMachine 属性应用于代码中的方法。 对于 Visual Basic 中具有 Iterator 修饰符的方法,编译器将在它发出的 IL 中应用 IteratorStateMachine 属性。
当 Visual Basic 中某个方法 (MethodName) 具有 Iterator 修饰符时,编译器会发出包含状态机结构的 IL。 该结构包含 方法中的代码。 该 IL 还包含调用状态机的 MethodName)  (存根方法。 编译器将 IteratorStateMachine 属性添加到存根方法,以便工具可以标识相应的状态机。 在编译器的未来版本中,发出的 IL 的详细信息可能会更改。
迭代器方法使用 Yield (Visual Basic) 语句对集合执行自定义迭代,一次返回一个元素。 有关更多信息,请参见 迭代器。
备注
不能使用 IteratorStateMachineAttribute 来测试方法是否为 C# 中的迭代器方法。
构造函数
| IteratorStateMachineAttribute(Type) | 初始化 IteratorStateMachineAttribute 类的新实例。 | 
属性
| StateMachineType | 返回编译器生成的实现状态机方法的基础状态机类型的类型对象。(继承自 StateMachineAttribute) | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |