I’m trying to determine why this type isn’t serializable (as tested by Type.IsSerializable())
<Serializable()> _
Public MustInherit Class WellKnownInstanceCollectionWithTypedId(Of T As WellKnownInstanceWithTypedId(Of IdT), IdT)
Inherits ReadOnlyCollection(Of T)
Public Sub New(ByVal list As IList(Of T))
MyBase.New(list)
End Sub
Public Function GetById(ByVal id As IdT) As T
Return Me.FirstOrDefault(Function(item) item.Id.Equals(id))
End Function
End Class
I know it has something to do with my GetById function, because if I remove that everything is fine. Can someone tell me what I need to change to have this type be serializable?
Update:
When I change my GetById implementation as such, everything is fine. Obviously this has something to do with Linq (as suggested below) – can anyone give me further details on why this is so?
Public Function GetById(ByVal id As IdT) As T
For Each i In Me
If i.Id.Equals(i) Then
Return i
End If
Next
Return Nothing
End Function
It could be related to the lambda expression (
Function) inGetById.Try converting it to an
AddressOfcall to see if the background closure VB.NET is creating for you is getting in the way of serialization.(Promoting comment to answer)
In your complete code, to test whether it is LINQ or the closure, just change your original code to
Return Me.FirstOrDefault(False).I believe that will make it serialisable again. If so, you can use
AddressOf id.Equalsinstead ofFunction(Item)...which has the same semantics unless you wanted to cater for a null (Nothing)id.