IteratorStateMachineAttribute 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
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
- 상속
- 특성
예제
다음 예제에서는 메서드가 반복기 메서드인지 여부를 확인하는 방법을 보여줍니다. 이 예제에서는 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 Iterator 의 메서드(MethodName)에 한정자가 있으면 컴파일러는 상태 컴퓨터 구조를 포함하는 IL을 내보낸다. 해당 구조체에는 메서드의 코드가 포함됩니다. 해당 IL에는 상태 머신을 호출하는 스텁 메서드(MethodName)도 포함되어 있습니다. 컴파일러는 도구가 IteratorStateMachine 해당 상태 컴퓨터를 식별할 수 있도록 스텁 메서드에 특성을 추가합니다. 내보낸 IL의 세부 정보는 컴파일러의 향후 릴리스에서 변경될 수 있습니다.
반복기 메서드는 (Visual Basic) 문을 사용하여 Yield 각 요소를 한 번에 하나씩 반환하여 컬렉션에 대한 사용자 지정 반복을 수행합니다.
메모
C#에서 메서드가 반복기 메서드인지 여부를 테스트하는 데 사용할 IteratorStateMachineAttribute 수 없습니다.
생성자
| Name | Description |
|---|---|
| IteratorStateMachineAttribute(Type) |
IteratorStateMachineAttribute 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| StateMachineType |
컴파일러가 상태 시스템 메서드를 구현하기 위해 생성한 기본 상태 컴퓨터 형식에 대한 형식 개체를 반환합니다. (다음에서 상속됨 StateMachineAttribute) |
| TypeId |
파생 클래스에서 구현되는 경우 이 Attribute대한 고유 식별자를 가져옵니다. (다음에서 상속됨 Attribute) |
메서드
| Name | Description |
|---|---|
| Equals(Object) |
이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| GetHashCode() |
이 인스턴스의 해시 코드를 반환합니다. (다음에서 상속됨 Attribute) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| IsDefaultAttribute() |
파생 클래스에서 재정의되는 경우 이 인스턴스의 값이 파생 클래스의 기본값인지 여부를 나타냅니다. (다음에서 상속됨 Attribute) |
| Match(Object) |
파생 클래스에서 재정의되는 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| _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) |