AsyncStateMachineAttribute 类
定义
public ref class AsyncStateMachineAttribute sealed : System::Runtime::CompilerServices::StateMachineAttribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class AsyncStateMachineAttribute : System.Runtime.CompilerServices.StateMachineAttribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
[System.Serializable]
public sealed class AsyncStateMachineAttribute : System.Runtime.CompilerServices.StateMachineAttribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type AsyncStateMachineAttribute = class
inherit StateMachineAttribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
[<System.Serializable>]
type AsyncStateMachineAttribute = class
inherit StateMachineAttribute
Public NotInheritable Class AsyncStateMachineAttribute
Inherits StateMachineAttribute
- 继承
- 属性
示例
如下面的示例所示,可以确定方法是否使用 Async 或 async 修饰符进行标记。As the following example shows, you can determine whether a method is marked with Async or async modifier. 在此示例中, IsAsyncMethod 执行以下步骤:In the example, IsAsyncMethod performs the following steps:
使用获取 MethodInfo 方法名称的对象 Type.GetMethod 。Obtains a MethodInfo object for the method name by using Type.GetMethod.
Type使用GetType 运算符或typeof获取属性的对象。Obtains a Type object for the attribute by using GetType Operator or typeof.
使用获取方法和属性类型的特性对象
MethodInfo.GetCustomAttribute。Obtains an attribute object for the method and attribute type by usingMethodInfo.GetCustomAttribute. 如果GetCustomAttributeNothingVisual Basic) 或null(c # ) 返回 (,则该方法不包含特性。IfGetCustomAttributereturnsNothing(Visual Basic) ornull(C#), the method doesn't contain the attribute.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace ConsoleApplication1
{
// This class is used by the code below to discover method attributes.
public class TheClass
{
public async Task<int> AsyncMethod()
{
await Task.Delay(5);
return 1;
}
public int RegularMethod()
{
return 0;
}
}
class Program
{
private static bool IsAsyncMethod(Type classType, string methodName)
{
// Obtain the method with the specified name.
MethodInfo method = classType.GetMethod(methodName);
Type attType = typeof(AsyncStateMachineAttribute);
// Obtain the custom attribute for the method.
// The value returned contains the StateMachineType property.
// Null is returned if the attribute isn't present for the method.
var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(attType);
return (attrib != null);
}
private static void ShowResult(Type classType, string methodName)
{
Console.Write((methodName + ": ").PadRight(16));
if (IsAsyncMethod(classType, methodName))
Console.WriteLine("Async method");
else
Console.WriteLine("Regular method");
}
static void Main(string[] args)
{
ShowResult(classType: typeof(TheClass), methodName: "AsyncMethod");
ShowResult(classType: typeof(TheClass), methodName: "RegularMethod");
// Note: The IteratorStateMachineAttribute applies to Visual Basic methods
// but not C# methods.
Console.ReadKey();
}
// Output:
// AsyncMethod: Async method
// RegularMethod: Regular method
}
}
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
注解
不应将属性应用于 AsyncStateMachine 代码中的方法。You shouldn't apply the AsyncStateMachine attribute to methods in your code. 对于具有 async 修饰符的方法,编译器将 AsyncStateMachine 在编译器发出的 IL 中应用特性。For methods that have the async modifier, the compiler will apply the AsyncStateMachine attribute in the IL that the compiler emits.
如果 (方法 名称 的方法) 具有 Async 或 async 修饰符,则编译器将发出包含状态机结构的 IL。When a method (MethodName) has the Async or async modifier, the compiler emits IL that includes a state machine structure. 此结构包含方法中的代码。This structure contains the code in the method. 该 IL 还包含一个存根方法, (方法 名称) 调用到状态机。That IL also contains a stub method (MethodName) that calls into the state machine. 编译器 AsyncStateMachine 会将属性添加到存根方法,以便工具能够识别相应的状态机。The compiler adds the AsyncStateMachine 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.
有关异步功能的信息,请参阅 异步编程 (c # ) 或异步编程 和 Await (Visual Basic) 。For information about the Async feature, see Asynchronous programming (C#) or Asynchronous programming with Async and Await (Visual Basic).
构造函数
| AsyncStateMachineAttribute(Type) |
初始化 AsyncStateMachineAttribute 类的新实例。Initializes a new instance of the AsyncStateMachineAttribute 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) |