완화된 대리자 변환(Visual Basic)

완화된 대리자 변환을 사용하면 서명이 동일하지 않은 경우에도 하위 및 함수를 대리자 또는 처리기에 할당할 수 있습니다. 따라서 대리자에 대한 바인딩은 메서드 호출에 이미 허용된 바인딩과 일치하게 됩니다.

매개 변수 및 반환 형식

정확한 서명 일치 대신, 완화된 변환을 위해서는 Option StrictOn으로 설정된 경우 다음 조건이 충족되어야 합니다.

  • 각 대리자 매개 변수의 데이터 형식에서 할당된 함수 또는 Sub의 해당 매개 변수의 데이터 형식으로 확대 변환이 존재해야 합니다. 다음 예제에서 대리자 Del1에는 하나의 매개 변수인 Integer가 있습니다. 할당된 람다 식의 매개 변수 m에는 Long 또는 Double과 같이 Integer에서 확장 변환이 있는 데이터 형식이 있어야 합니다.

    ' Definition of delegate Del1.
    Delegate Function Del1(ByVal arg As Integer) As Integer
    
    ' Valid lambda expression assignments with Option Strict on or off:
    
    ' Integer matches Integer.
    Dim d1 As Del1 = Function(m As Integer) 3
    
    ' Integer widens to Long
    Dim d2 As Del1 = Function(m As Long) 3
    
    ' Integer widens to Double
    Dim d3 As Del1 = Function(m As Double) 3
    

    축소 변환은 Option StrictOff로 설정된 경우에만 허용됩니다.

    ' Valid only when Option Strict is off:
    
    Dim d4 As Del1 = Function(m As String) CInt(m)
    Dim d5 As Del1 = Function(m As Short) m
    
  • 확대 변환은 할당된 함수의 반환 형식 또는 Sub 대리자의 반환 형식과 반대 방향으로 존재해야 합니다. 다음 예제에서 del1의 반환 형식이 Integer이므로 할당된 각 람다 식의 본문은 Integer로 확장되는 데이터 형식으로 평가되어야 합니다.

    ' Valid return types with Option Strict on:
    
    ' Integer matches Integer.
    Dim d6 As Del1 = Function(m As Integer) m
    
    ' Short widens to Integer.
    Dim d7 As Del1 = Function(m As Long) CShort(m)
    
    ' Byte widens to Integer.
    Dim d8 As Del1 = Function(m As Double) CByte(m)
    

Option StrictOff로 설정된 경우 확장 제한이 양방향으로 제거됩니다.

' Valid only when Option Strict is set to Off.

' Integer does not widen to Short in the parameter.
Dim d9 As Del1 = Function(n As Short) n

' Long does not widen to Integer in the return type.
Dim d10 As Del1 = Function(n As Integer) CLng(n)

매개 변수 사양 생략

완화된 대리자를 사용하면 할당된 메서드에서 매개 변수 사양을 완전히 생략할 수도 있습니다.

' Definition of delegate Del2, which has two parameters.
Delegate Function Del2(ByVal arg1 As Integer, ByVal arg2 As String) As Integer
' The assigned lambda expression specifies no parameters, even though
' Del2 has two parameters. Because the assigned function in this 
' example is a lambda expression, Option Strict can be on or off.
' Compare the declaration of d16, where a standard function is assigned.
Dim d11 As Del2 = Function() 3

' The parameters are still there, however, as defined in the delegate.
Console.WriteLine(d11(5, "five"))

' Not valid.
' Console.WriteLine(d11())
' Console.WriteLine(d11(5))

일부 매개 변수는 지정하고 다른 매개 변수는 생략할 수 없습니다.

' Not valid.
'Dim d12 As Del2 = Function(p As Integer) p

매개 변수를 생략하는 기능은 여러 개의 복잡한 매개 변수가 관련되어 있는 이벤트 처리기를 정의하는 등의 상황에서 유용합니다. 일부 이벤트 처리기에 대한 인수는 사용되지 않습니다. 대신 처리기는 이벤트가 등록된 컨트롤의 상태에 직접 액세스하고 인수를 무시합니다. 완화된 대리자를 사용하면 모호성이 발생하지 않는 경우 해당 선언에서 인수를 생략할 수 있습니다. 다음 예제에서는 완전히 지정된 메서드 OnClickRelaxedOnClick으로 다시 작성할 수 있습니다.

Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles b.Click  
    MessageBox.Show("Hello World from" + b.Text)  
End Sub  
  
Sub RelaxedOnClick() Handles b.Click  
    MessageBox.Show("Hello World from" + b.Text)  
End Sub  

AddressOf 예제

람다 식은 이전 예제에서 형식 관계를 쉽게 볼 수 있도록 하기 위해 사용되었습니다. 그러나 AddressOf, Handles 또는 AddHandler를 사용하는 대리자 할당에 대해서도 동일한 완화가 허용됩니다.

다음 예제에서는 f1, f2, f3, f4 함수를 모두 Del1에 할당할 수 있습니다.

' Definition of delegate Del1.
Delegate Function Del1(ByVal arg As Integer) As Integer
' Definitions of f1, f2, f3, and f4.
Function f1(ByVal m As Integer) As Integer
End Function

Function f2(ByVal m As Long) As Integer
End Function

Function f3(ByVal m As Integer) As Short
End Function

Function f4() As Integer
End Function
' Assignments to function delegate Del1.

' Valid AddressOf assignments with Option Strict on or off:

' Integer parameters of delegate and function match.
Dim d13 As Del1 = AddressOf f1

' Integer delegate parameter widens to Long.
Dim d14 As Del1 = AddressOf f2

' Short return in f3 widens to Integer.
Dim d15 As Del1 = AddressOf f3

다음 예는 Option StrictOff로 설정된 경우에만 유효합니다.

' If Option Strict is Off, parameter specifications for f4 can be omitted.
Dim d16 As Del1 = AddressOf f4

' Function d16 still requires a single argument, however, as specified
' by Del1.
Console.WriteLine(d16(5))

' Not valid.
'Console.WriteLine(d16())
'Console.WriteLine(d16(5, 3))

함수 반환 끊기

완화된 대리자 변환을 사용하면 함수를 Sub 대리자에 할당하여 함수의 반환 값을 효과적으로 무시할 수 있습니다. 그러나 함수 대리자에 Sub을 할당할 수는 없습니다. 다음 예제에서는 doubler 함수의 주소가 Sub 대리자 Del3에 할당됩니다.

' Definition of Sub delegate Del3.
Delegate Sub Del3(ByVal arg1 As Integer)

' Definition of function doubler, which both displays and returns the
' value of its integer parameter.
Function doubler(ByVal p As Integer) As Integer
    Dim times2 = 2 * p
    Console.WriteLine("Value of p: " & p)
    Console.WriteLine("Double p:   " & times2)
    Return times2
End Function
' You can assign the function to the Sub delegate:
Dim d17 As Del3 = AddressOf doubler

' You can then call d17 like a regular Sub procedure.
d17(5)

' You cannot call d17 as a function. It is a Sub, and has no 
' return value.
' Not valid.
'Console.WriteLine(d17(5))

참고 항목