I have an inheritance structure: Fooimplements IGraphNodeinherits IGraphItem.
Foo, IGraphItem/ IGraphNode, and the implementation for IGraphItem/ is IGraphNodeall in separate assemblies. I use the inversion of the control container, so the project I'm working on has a link to the first two ( Fooand IGraphItem/ IGraphNode), but not to the IGraphItem/ implementation IGraphNode. I also have the Strict option, as required for this project (if disabled, this will not fix problem). I am using .NET 3.5.
I pass IGraphItemand I have code that looks like this:
Public Sub ProcessItem(of IGraphItem)(item As IGraphItem)
If TypeOf item Is Foo Then
Dim f1 = CType(item, Foo) 'Compiler error
Dim f2 = DirectCast(item, Foo) 'Compiler error
'This is what I'm currently having to do. It works.
Dim f = CType(CType(item, IGraphNode), Foo)
'Do stuff
End If
End Sub
, ? , TryCast , , item Foo, , DirectCast. , ? , ?