AsyncStateMachineAttribute Klasse

Definition

Gibt an, ob eine Methode entweder mit dem Async - oder asynchronen Modifizierer markiert ist.

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
Vererbung
AsyncStateMachineAttribute
Attribute

Beispiele

Wie im folgenden Beispiel gezeigt, können Sie bestimmen, ob eine Methode mit Async oder asynchronem Modifizierer markiert ist. Führen Sie im Beispiel IsAsyncMethod die folgenden Schritte aus:

  • Ruft ein MethodInfo Objekt für den Methodennamen mithilfe Type.GetMethodvon .

  • Ruft ein Type Objekt für das Attribut mithilfe von GetType-Operator oder Typeof ab.

  • Ruft ein Attributobjekt für die Methode und den Attributtyp mithilfe von MethodInfo.GetCustomAttribute. Wenn GetCustomAttributeNothing (Visual Basic) oder null (C#) zurückgibt, enthält die Methode nicht das Attribut.

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

Hinweise

Sie sollten das AsyncStateMachine Attribut nicht auf Methoden in Ihrem Code anwenden. Bei Methoden mit dem asynchronen Modifizierer wendet der Compiler das AsyncStateMachine Attribut in der IL an, die der Compiler ausgibt.

Wenn eine Methode (MethodName) den Async- oder asynchronen Modifizierer aufweist, gibt der Compiler IL aus, der eine Zustandsautomatstruktur enthält. Diese Struktur enthält den Code in der Methode. Diese IL enthält auch eine Stubmethode (MethodName), die den Zustandsautomaten aufruft. Der Compiler fügt das AsyncStateMachine Attribut der Stubmethode hinzu, sodass Tools den entsprechenden Zustandsautomaten identifizieren können. Details der ausgegebenen IL können sich in zukünftigen Versionen der Compiler ändern.

Informationen zum Async-Feature finden Sie unter Asynchrone Programmierung (C#) oder Asynchrone Programmierung mit Async und Await (Visual Basic).

Konstruktoren

Name Beschreibung
AsyncStateMachineAttribute(Type)

Initialisiert eine neue Instanz der AsyncStateMachineAttribute-Klasse.

Eigenschaften

Name Beschreibung
StateMachineType

Gibt das Typobjekt für den zugrunde liegenden Zustandsautomatentyp zurück, der vom Compiler generiert wurde, um die Zustandsautomatmethode zu implementieren.

(Geerbt von StateMachineAttribute)
TypeId

Wenn sie in einer abgeleiteten Klasse implementiert wird, wird ein eindeutiger Bezeichner für diese Attribute.

(Geerbt von Attribute)

Methoden

Name Beschreibung
Equals(Object)

Gibt einen Wert zurück, der angibt, ob diese Instanz einem angegebenen Objekt entspricht.

(Geerbt von Attribute)
GetHashCode()

Gibt den Hashcode für diese Instanz zurück.

(Geerbt von Attribute)
GetType()

Ruft die Type der aktuellen Instanz ab.

(Geerbt von Object)
IsDefaultAttribute()

Wenn sie in einer abgeleiteten Klasse überschrieben wird, gibt an, ob der Wert dieser Instanz der Standardwert für die abgeleitete Klasse ist.

(Geerbt von Attribute)
Match(Object)

Wenn sie in einer abgeleiteten Klasse überschrieben wird, wird ein Wert zurückgegeben, der angibt, ob diese Instanz einem angegebenen Objekt entspricht.

(Geerbt von Attribute)
MemberwiseClone()

Erstellt eine flache Kopie der aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Explizite Schnittstellenimplementierungen

Name Beschreibung
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Ordnet eine Reihe von Namen einer entsprechenden Reihe von Dispatchbezeichnern zu.

(Geerbt von Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Ruft die Typinformationen für ein Objekt ab, mit denen die Typinformationen für eine Schnittstelle abgerufen werden können.

(Geerbt von Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Ruft die Anzahl der Schnittstellen mit Typinformationen ab, die von einem Objekt bereitgestellt werden (0 oder 1).

(Geerbt von Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Bietet Zugriff auf Eigenschaften und Methoden, die von einem Objekt verfügbar gemacht werden.

(Geerbt von Attribute)

Gilt für:

Weitere Informationen