AsyncStateMachineAttribute Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
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
- Öröklődés
- Attribútumok
Példák
Ahogy az alábbi példa is mutatja, meghatározhatja, hogy egy metódus Async vagy aszinkron módosítóval van-e megjelölve. A példában IsAsyncMethod a következő lépéseket hajtja végre:
MethodInfo A metódusnév objektumának lekérése a következő használatávalType.GetMethod: .
Az attribútum objektumának Type lekérése a GetType operátor vagy típustípus használatával.
A metódushoz és az attribútumtípushoz egy attribútumobjektumot kér le a használatával
MethodInfo.GetCustomAttribute. HaGetCustomAttributeNothing(Visual Basic) vagynull(C#) értéket ad vissza, a metódus nem tartalmazza az attribútumot.
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
Megjegyzések
Ne alkalmazza az attribútumot a AsyncStateMachine kód metódusaira. Az aszinkron módosítóval rendelkező metódusok esetében a fordító a fordító által kibocsátott IL-ben alkalmazza AsyncStateMachine az attribútumot.
Ha egy metódus (MethodName) aszinkron vagy aszinkron módosítóval rendelkezik, a fordító egy állapotgép-struktúrát tartalmazó IL-t bocsát ki. Ez a struktúra tartalmazza a metódus kódját. Ez az IL tartalmaz egy csonkmetódust (MethodName), amely meghívja az állapotgépet. A fordító hozzáadja az attribútumot a AsyncStateMachine csonk metódushoz, hogy az eszközök azonosítani tudják a megfelelő állapotgépet. A kibocsátott IL részletei változhatnak a fordítók későbbi kiadásaiban.
Az Async szolgáltatással kapcsolatos információkért lásd: Aszinkron programozás (C#) vagy Aszinkron programozás az Async és a Await (Visual Basic).
Konstruktorok
| Name | Description |
|---|---|
| AsyncStateMachineAttribute(Type) |
Inicializálja a AsyncStateMachineAttribute osztály új példányát. |
Tulajdonságok
| Name | Description |
|---|---|
| StateMachineType |
Az állapotgép-metódus implementálásához a fordító által létrehozott mögöttes állapotgép-típus típusobjektumát adja vissza. (Öröklődés forrása StateMachineAttribute) |
| TypeId |
Ha származtatott osztályban implementálják, ehhez egy egyedi azonosítót Attributekap. (Öröklődés forrása Attribute) |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Olyan értéket ad vissza, amely jelzi, hogy ez a példány egyenlő-e egy adott objektummal. (Öröklődés forrása Attribute) |
| GetHashCode() |
A példány kivonatkódját adja vissza. (Öröklődés forrása Attribute) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| IsDefaultAttribute() |
Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy a példány értéke-e a származtatott osztály alapértelmezett értéke. (Öröklődés forrása Attribute) |
| Match(Object) |
Származtatott osztály felülírásakor egy olyan értéket ad vissza, amely jelzi, hogy ez a példány egy adott objektummal egyenlő-e. (Öröklődés forrása Attribute) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |
Explicit interfész-implementációk
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Névkészletet képez le a küldési azonosítók megfelelő készletére. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Lekéri egy objektum típusadatait, amelyek a felület típusadatainak lekérésére használhatók. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Lekéri az objektumok által biztosított típusinformációs felületek számát (0 vagy 1). (Öröklődés forrása Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Hozzáférést biztosít az objektumok által közzétett tulajdonságokhoz és metódusokhoz. (Öröklődés forrása Attribute) |