Question Why Inherited Interface not Work?

yxq

Member
Joined
Jan 22, 2007
Messages
5
Programming Experience
1-3
I am testing the NTFS Reader from
GitHub - michaelkc/NtfsReader: Danny Coutures NtfsReader source (fast NTFS access, like everything)

The dll file has been attached.

But why the code below will not work? Thank you.

VB.NET:
Public Interface INodeNew
     Inherits INode
End Interface

Public Sub GetItems()
     Dim listValue1 As List(Of INode)
     Dim listValue2 As List(Of INodeNew)
     Dim driveToAnalyze As New DriveInfo("C")
     Dim ntfsReader As New NtfsReader(driveToAnalyze, RetrieveMode.All)

     listValue1 = ntfsReader.GetNodes(driveToAnalyze.Name) ' This line works well
     MessageBox.Show(listValue1.Count)

     listValue2 = listValue1.Cast(Of INodeNew).ToList ' This line will not work,
     MessageBox.Show(listValue2.Count)
End Sub
 

Attachments

  • NtfsReader.zip
    13.1 KB · Views: 3
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
That GetNodes is being executed in a place that knows absolutely nothing about your INodeNew interface. It is returning objects that implement INode. Your inheriting INode in INodeNew doesn't magically make any object that implements INode also implement INodeNew. How could it? What if you were to add some members to that INodeNew interface? How could the objects returned by GetNodes possibly implement those members without any knowledge that they even exist?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
No it can't. Look at this:
VB.NET:
Module Module1

    Sub Main()
        Dim b1 = {New Base, New Base}
        Dim d1 = {New Derived, New Derived}

        Dim b2 = d1.Cast(Of IBase).ToArray()
        Dim d2 = d1.Cast(Of IDerived).ToArray()

        Dim b3 = b1.Cast(Of IBase).ToArray()
        Dim d3 = b1.Cast(Of IDerived).ToArray()
    End Sub

End Module

Public Interface IBase

End Interface

Public Interface IDerived
    Inherits IBase

End Interface

Public Class Base
    Implements IBase

End Class

Public Class Derived
    Implements IDerived

End Class
What would you expect to happen there and why? What you're trying to do fails for the same reason. You can't cast an object as a type that it isn't.
 

yxq

Member
Joined
Jan 22, 2007
Messages
5
Programming Experience
1-3
Only can use the For each to covert the INode to a new Class? it's slow for lots of items.
Because i want to invoke the ntfsreader.dll in A.dll, build a function to return list of
(INode) value in A.dll, then B program invoke A.dll to get the list of (INode), how to return the INode value in B program without referencing the ntfsreader.dll?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
If INode is declared in ntfsreader.dll then you can't. If you want a project to use a type then that project has to know that that type exists. How would that happen without referencing the library that the type is declared in?
 

yxq

Member
Joined
Jan 22, 2007
Messages
5
Programming Experience
1-3
You mean that the ntfsreader.dll must be referenced in B program for using the INode to get the return value from A dll?
 

Latest posts

Top Bottom