IteratorStateMachineAttribute 类
定义
指示是否使用 Iterator 修饰符标记 Visual Basic 中的方法。Indicates whether a method in Visual Basic is marked with the Iterator modifier.
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
- 继承
- 属性
示例
下面的示例演示如何确定某个方法是否为迭代器方法。The following example shows how to determine whether a method is an iterator method. 在此示例中, IsIteratorMethod 执行以下步骤:In the example, IsIteratorMethod performs the following steps:
使用获取 MethodInfo 方法名称的对象 Type.GetMethod 。Obtains a MethodInfo object for the method name by using Type.GetMethod.
Type使用GetType 运算符获取属性的对象。Obtains a Type object for the attribute by using GetType Operator.
使用获取方法和属性类型的特性对象
MethodInfo.GetCustomAttribute。Obtains an attribute object for the method and attribute type by usingMethodInfo.GetCustomAttribute. 如果GetCustomAttribute返回Nothing(Visual Basic) ,则该方法不包含特性。IfGetCustomAttributereturnsNothing(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
注解
不应将属性应用于 IteratorStateMachine 代码中的方法。You shouldn't apply the IteratorStateMachine attribute to methods in your code. 对于包含 Iterator 修饰符的 Visual Basic 中的方法,编译器将 IteratorStateMachine 在其发出的 IL 中应用特性。For methods in Visual Basic that have the Iterator modifier, the compiler will apply the IteratorStateMachine attribute in the IL that it emits.
如果 Visual Basic 具有修饰符的方法中 (方法 名称) Iterator ,则编译器将发出包含状态机结构的 IL。When a method (MethodName) in Visual Basic has the Iterator modifier, the compiler emits IL that includes a state machine structure. 该结构包含方法中的代码。That structure contains the code in the method. 该 IL 还包含一个存根方法, (方法 名称) 调用到状态机。That IL also contains a stub method (MethodName) that calls into the state machine. 编译器 IteratorStateMachine 会将属性添加到存根方法,以便工具能够识别相应的状态机。The compiler adds the IteratorStateMachine attribute to the stub method so that tools can identify the corresponding state machine. 在未来版本的编译器中,发出的 IL 的详细信息可能会发生变化。Details of the emitted IL might change in future releases of the compilers.
迭代器方法通过使用 Yield (Visual Basic) 语句每次返回一个元素,对集合执行自定义迭代。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. 有关更多信息,请参见 迭代器。For more information, see Iterators.
备注
不能使用 IteratorStateMachineAttribute 测试方法是否是 c # 中的迭代器方法。You can't use IteratorStateMachineAttribute to test whether a method is an iterator method in C#.
构造函数
| IteratorStateMachineAttribute(Type) |
初始化 IteratorStateMachineAttribute 类的新实例。Initializes a new instance of the IteratorStateMachineAttribute class. |
属性
| StateMachineType |
返回编译器生成的实现状态机方法的基础状态机类型的类型对象。Returns the type object for the underlying state machine type that was generated by the compiler to implement the state machine method. (继承自 StateMachineAttribute) |
| TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute. (继承自 Attribute) |
方法
| Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object. (继承自 Attribute) |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. (继承自 Attribute) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |