L
Leon Mayne
This is probably a bit of a n00b question, but here goes.
I have an interface in my business layer called IArchivable which includes
functions and properties for 'deleting' an object in the database without
actually deleting it. My functions in the business layer which return lists
of these objects always return all objects, whether they have been archived
or not (in case we want to display the deleted items to allow the user to
undelete them). I want to create a function which will filter out the
archived objects, and I'm using .NET 2.0 so I can't use LINQ.
I created a function with the following signature, which works fine:
Public Shared Function FilterArchived(Of T)(ByVal pList As Generic.List(Of
T)) As Generic.IList(Of T)
However, because the parameter is of T it's possible that someone could pass
in a collection of objects that does not implement IArchivable. Therefore I
tried changing the signature to:
Public Shared Function FilterArchived(Of T)(ByVal pList As Generic.List(Of
IArchivable)) As Generic.IList(Of T)
But this throws a runtime exception:
System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type
'System.Collections.Generic.List`1[Bss.Target100.Europa.SafetyPolicySection]'
to type
'System.Collections.Generic.List`1[Bss.Target100.Europa.IArchivable]'."
(SafetyPolicySection implements IArchivable)
It would seem that the compiler is not smart enough to work out that the
container type implements the interface and cannot cast the object type. Is
there a way round this?
I have an interface in my business layer called IArchivable which includes
functions and properties for 'deleting' an object in the database without
actually deleting it. My functions in the business layer which return lists
of these objects always return all objects, whether they have been archived
or not (in case we want to display the deleted items to allow the user to
undelete them). I want to create a function which will filter out the
archived objects, and I'm using .NET 2.0 so I can't use LINQ.
I created a function with the following signature, which works fine:
Public Shared Function FilterArchived(Of T)(ByVal pList As Generic.List(Of
T)) As Generic.IList(Of T)
However, because the parameter is of T it's possible that someone could pass
in a collection of objects that does not implement IArchivable. Therefore I
tried changing the signature to:
Public Shared Function FilterArchived(Of T)(ByVal pList As Generic.List(Of
IArchivable)) As Generic.IList(Of T)
But this throws a runtime exception:
System.InvalidCastException was unhandled by user code
Message="Unable to cast object of type
'System.Collections.Generic.List`1[Bss.Target100.Europa.SafetyPolicySection]'
to type
'System.Collections.Generic.List`1[Bss.Target100.Europa.IArchivable]'."
(SafetyPolicySection implements IArchivable)
It would seem that the compiler is not smart enough to work out that the
container type implements the interface and cannot cast the object type. Is
there a way round this?