완화된 대리자 변환을 사용하면 서명이 동일하지 않은 경우에도 대리자 또는 처리기에 하위 및 함수를 할당할 수 있습니다. 따라서 대리자 바인딩은 메서드 호출에 이미 허용된 바인딩과 일치하게 됩니다.
매개 변수 및 반환 형식
정확한 서명 일치 대신 완화된 변환을 사용하려면 Option Strict
을 On
으로 설정할 때 다음 조건을 충족해야 합니다.
각 대리자 매개 변수의 데이터 형식과 할당된 함수의 해당 매개 변수의 데이터 형식 간에 반드시 확대 변환이 존재해야 합니다. 다음 예에서 델리게이트
Del1
는 매개 변수 하나인Integer
를 포함하고 있습니다. 할당된 람다 식의 매개변수m
는Integer
로부터 확대 변환이 가능한 데이터 형식이어야 하며, 예를 들어Long
또는Double
와 같은 형식이 필요합니다.' 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 Strict
경우에만Off
허용됩니다.' 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
대리자의 반환 형식과 반대 방향으로 존재해야 합니다. 다음 예제에서 할당된 각 람다 식의 본문은 반환 형식이Integer
인del1
이기 때문에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 Strict
설정이 Off
인 경우, 확대 제한이 양방향으로 제거됩니다.
' 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
매개 변수를 생략하는 기능은 여러 복잡한 매개 변수가 관련된 이벤트 처리기 정의와 같은 상황에서 유용합니다. 일부 이벤트 처리기에 대한 인수는 사용되지 않습니다. 대신 처리기는 이벤트가 등록된 컨트롤의 상태에 직접 액세스하고 인수를 무시합니다. 간소화된 대리자를 사용하면 모호성이 없는 경우 이러한 선언에서 인수를 생략할 수 있습니다. 다음 예제에서는 완전히 지정된 메서드 OnClick
를 .로 RelaxedOnClick
다시 작성할 수 있습니다.
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 Strict
가 Off
으로 설정된 경우에만 유효합니다.
' 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))
참고하십시오
.NET