Why do I need to duplicate here?

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. , ? , ?

+3
2

, - 3.5.

, , IGraphItem - , T, . , T, Foo.

- , , :

Public Sub ProcessItem(of IGraphItm)(item As IGraphItem)

, - "" IGraphItem IGraphItem .

, , item As IGraphItem item As YourNamespace.IGraphItem.

, , , , ;)

+2

All Articles