IteratorStateMachineAttribute 클래스

정의

Visual Basic의 메서드가 Iterator 한정자로 표시되었는지 여부를 나타냅니다.

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
상속
IteratorStateMachineAttribute
특성

예제

다음 예제에서는 메서드가 반복기 메서드인지 여부를 확인하는 방법을 보여줍니다. 이 예제에서는 IsIteratorMethod 다음 단계를 수행합니다.

  • MethodInfo 사용하여 Type.GetMethod메서드 이름에 대한 개체를 가져옵니다.

  • GetType 연산자를 Type 사용하여 특성에 대한 개체를 가져옵니다.

  • 를 사용하여 MethodInfo.GetCustomAttribute메서드 및 특성 형식에 대한 특성 개체를 가져옵니다. 반환 Nothing 되는 경우GetCustomAttribute(Visual Basic) 메서드에 특성이 포함되지 않습니다.

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 특성을 적용하면 안 됩니다. 반복기 한정자가 있는 Visual Basic 메서드의 경우 컴파일러는 내보내는 IL에 특성을 적용 IteratorStateMachine 합니다.

Visual Basic 메서드(MethodName)에 한정자가 있으면 Iterator 컴파일러는 상태 컴퓨터 구조를 포함하는 IL을 내보낸다. 해당 구조에는 메서드의 코드가 포함됩니다. 해당 IL에는 상태 머신을 호출하는 스텁 메서드(MethodName)도 포함됩니다. 컴파일러는 도구가 IteratorStateMachine 해당 상태 컴퓨터를 식별할 수 있도록 스텁 메서드에 특성을 추가합니다. 내보낸 IL의 세부 정보는 컴파일러의 이후 릴리스에서 변경될 수 있습니다.

반복기 메서드는 (Visual Basic) 문을 사용하여 Yield 컬렉션에 대해 사용자 지정 반복을 수행하여 각 요소를 한 번에 하나씩 반환합니다. 자세한 내용은 반복기를 참조하세요.

참고

C#에서 메서드가 반복기 메서드인지 여부를 테스트하는 데 사용할 IteratorStateMachineAttribute 수 없습니다.

생성자

IteratorStateMachineAttribute(Type)

IteratorStateMachineAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

StateMachineType

상태 컴퓨터 메서드를 구현하기 위해 컴파일러에서 생성되었던 기본 상태 컴퓨터 유형에 대한 유형 개체를 반환합니다.

(다음에서 상속됨 StateMachineAttribute)
TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Equals(Object)

이 인스턴스가 지정된 개체와 같은지를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의된 경우 이 인스턴스 값이 파생 클래스에 대한 기본값인지 여부를 표시합니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의된 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

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

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상

추가 정보