-
Mar 24th, 2023, 06:53 AM
#1
Thread Starter
Junior Member
Interface Comparer in .Net Fx 2.0 vs .Net Fx 4.x
I've got something like this code in a library assembly:
Code:
Public Interface IPlanable
Property XYZ As String
End Interface
Public Class SomePlanableItem
Implements IPlanable
Public Property A As String Implements IPlanable.XYZ
End Class
Public Class PlanableComparer
Inherits Comparer(Of IPlanable)
Public Overrides Function Compare(x As IPlanable, y As IPlanable) As Integer
Return x.XYZ.CompareTo(y.XYZ)
End Function
End Class
Module Module1
Sub Main()
Dim myList As New List(Of SomePlanableItem)
myList.Add(New SomePlanableItem With {.A = "Foo"})
myList.Sort(New PlanableComparer)
End Sub
End Module
(Of course the real code does not use Module1/Main. I changed this for isolating the problem in a console application. - You can copy&paste the above into a console app to reproduce the issue.)
The above code runs as expected in .Net Fx 4.*
However, if I run it in .Net Fx 2.0, I get an InvalidCastException: Unable to cast object of type 'Stuff.Types.PlanableComparer' to type 'System.Collections.Generic.IComparer`1[Stuff.Types.SomePlanableItem]'.
It does work with .Net 2.0, if I implement IComparer(Of SomePlanableItem) in my PlanableComparer. E.g.:
Code:
Public Class PlanableComparer
Inherits Comparer(Of IPlanable)
Implements IComparer(Of SomePlanableItem)
Public Overrides Function Compare(x As IPlanable, y As IPlanable) As Integer
Return x.XYZ.CompareTo(y.XYZ)
End Function
Public Function CompareSomePlanableItem(x As SomePlanableItem, y As SomePlanableItem) As Integer _
Implements IComparer(Of SomePlanableItem).Compare
Return Me.Compare(x, y)
End Function
End Class
But that would undermine the use of the interface, as I would need to implement a Compare method for each type.
Not sure what to do here. Thoughts?
Last edited by sonic8; Mar 24th, 2023 at 07:00 AM.
-
Mar 24th, 2023, 10:26 AM
#2
Re: Interface Comparer in .Net Fx 2.0 vs .Net Fx 4.x
Framework 2.0 was probably the first good framework for .NET. Anything before that could probably considered ".NET beta". However, FW2.0 was also lacking some major features that got added with FW3.0 or 3.5, and one of them is probably the key to your issue. The versions from 1 to 2 to 3 to 4 each included some significant advances in the technology of the language, though I'd say that the big advances that version 4.x added largely involved Tasks and threading conveniences, which probably aren't relevant to your issue.
What I think would be best is to abandon FW2.0, but that might not be something you can do. After all, FW3.5 added LINQ, lambdas, and much else, while FW4.0 added Tasks, so if you limit yourself to what was available only in FW2.0 then you are leaving out some big items.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|