각 클래스에는 Sequence라는 속성이 있습니다. 이 속성은 ISequenced라는 인터페이스에서 구현됩니다. 이 예제에서는 다음 클래스 중 하나를 호출 할 수 있습니다.
List (of)가있을 때 표준 List.Sort (addressof delegate)를 사용하여 정렬 할 수 있기를 원합니다. 여기서 대리자는 표준 함수입니다. ISequenced를 구현하는 각 개별 클래스에 대한 함수를 선언하는 대신 두 개의 ISequenced 개체에 시퀀스 번호를 비교하고 부울 플래그를 반환합니다.일반적인 형식의 대리자 및 인터페이스에 리플렉션 사용
예.
Dim li as List(of A) = GetValues()
li.Sort(addressof SortBySeq)
...
Public Function SortBySeq(ByVal ob1 as ISequenced, ByVal ob2 as ISequenced) as Boolean
return ob1.Sequence.CompareTo(ob2.Sequence)
End If
편집 : 위의 사용하는 것은 나에게 다음과 같은 오류 제공 : 나는 그것을 VB.NET에서도 가능하면 (이 작업을 수행하기 위해 함수를 선언 할 방법
"Overload resolution failed because no accessible 'Sort' can be called with these arguments: 'Public Sub Sort(comparison As System.Comparison(Of A))': Option Strict On does not allow narrowing in implicit type conversions between method 'Public Function SortBySeq(ob1 As ISequenced, ob2 As ISequenced) As Integer' and delegate 'Delegate Function Comparison(Of A)(x As A, y As A) As Integer'. 'Public Sub Sort(comparer As System.Collections.Generic.IComparer(Of A))': 'AddressOf' expression cannot be converted to 'System.Collections.Generic.IComparer(Of A)' because 'System.Collections.Generic.IComparer(Of A)' is not a delegate type."
을
감사합니다, 데인.
IComparer를 사용하면 동일한 클래스의 두 객체를 비교할 수 있지만 ISequenced 인터페이스를 사용하는 각 클래스에 대해 함수를 구현하지 않아도됩니다. 나는 당신에게 내가하려고하는 것에 대한 더 나은 생각을 줄 수있는 코드 샘플을 가지고 나의 질문을 확장했다. – link664
그건 인터페이스의 아름다움입니다. 위의 편집을 참조하십시오. – xanadont
나는 그것이 정말로 IHasSequence이어야한다고 생각한다. – xanadont