IteratorStateMachineAttribute Sınıf

Tanım

Visual Basic içindeki bir yöntemin Iterator değiştiriciyle işaretlenip işaretlenmediğini gösterir.

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
Devralma
IteratorStateMachineAttribute
Öznitelikler

Örnekler

Aşağıdaki örnekte bir yöntemin yineleyici yöntem olup olmadığının nasıl belirleneceği gösterilmektedir. Örnekte aşağıdaki IsIteratorMethod adımları gerçekleştirir:

  • kullanarak Type.GetMethodyöntem adı için bir MethodInfo nesnesi alır.

  • GetType İşleci'ni kullanarak özniteliği için bir Type nesnesi alır.

  • kullanarak MethodInfo.GetCustomAttributeyöntemi ve öznitelik türü için bir öznitelik nesnesi alır. döndürürse GetCustomAttribute Nothing (Visual Basic), yöntemi özniteliğini içermez.

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

Açıklamalar

özniteliğini IteratorStateMachine kodunuzdaki yöntemlere uygulamamalısınız. Visual Basic'de Yineleyici değiştiricisi olan yöntemler için derleyici, yaydığı IL'de özniteliğini uygularIteratorStateMachine.

Visual Basic'daki bir yöntemin (MethodName) değiştiricisi Iterator olduğunda, derleyici durum makinesi yapısını içeren IL'yi yayar. Bu yapı yöntemindeki kodu içerir. Bu IL ayrıca durum makinesine çağrıda bulunan bir saptama yöntemi (MethodName) içerir. Derleyici, araçların karşılık gelen durum makinesini tanımlayabilmesi için özniteliğini saplama yöntemine ekler IteratorStateMachine . Yayılan IL'nin ayrıntıları, derleyicilerin gelecek sürümlerinde değişebilir.

Yineleyici yöntemi, her öğeyi birer birer döndürmek için (Visual Basic) deyimini Yield kullanarak bir koleksiyon üzerinde özel yineleme gerçekleştirir. Daha fazla bilgi için bkz. Yineleyiciler.

Not

Bir yöntemin C# dilinde yineleyici bir yöntem olup olmadığını test etmek için kullanamazsınız IteratorStateMachineAttribute .

Oluşturucular

IteratorStateMachineAttribute(Type)

IteratorStateMachineAttribute sınıfının yeni bir örneğini başlatır.

Özellikler

StateMachineType

Durum makinesi yöntemini uygulamak için derleyici tarafından oluşturulan temel durum makine türü için tür nesnesini döndürür.

(Devralındığı yer: StateMachineAttribute)
TypeId

Türetilmiş bir sınıfta uygulandığında, bu Attributeiçin benzersiz bir tanımlayıcı alır.

(Devralındığı yer: Attribute)

Yöntemler

Equals(Object)

Bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değeri döndürür.

(Devralındığı yer: Attribute)
GetHashCode()

Bu örneğe ilişkin karma kodu döndürür.

(Devralındığı yer: Attribute)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
IsDefaultAttribute()

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin değerinin türetilmiş sınıf için varsayılan değer olup olmadığını gösterir.

(Devralındığı yer: Attribute)
Match(Object)

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değer döndürür.

(Devralındığı yer: Attribute)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Belirtik Arabirim Kullanımları

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

Bir ad kümesini karşılık gelen bir dağıtma tanımlayıcısı kümesine eşler.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Bir arabirimin tür bilgilerini almak için kullanılabilecek bir nesnenin tür bilgilerini alır.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Bir nesnenin sağladığı tür bilgisi arabirimlerinin sayısını alır (0 ya da 1).

(Devralındığı yer: Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Bir nesne tarafından sunulan özelliklere ve yöntemlere erişim sağlar.

(Devralındığı yer: Attribute)

Şunlara uygulanır

Ayrıca bkz.