When a class is assigned to an object and you want to cast the object value to an inherited interface of the original class, an InvalidCastException is raised. For the life of me, I cannot figure out why. I am new to NetMF code, so I am assuming that I am doing something very wrong.
This works in the normal .Net code.
Option Explicit On Option Strict On Imports Microsoft.SPOT Namespace InterfaceTest Public Module Module1 Sub Main() Dim obj As Object Dim test As IBase Dim level1 As ILevel1 Dim local As New Level1 ' Exercise the instances. local.BaseTest() level1 = local level1.BaseTest() test = level1 test.BaseTest() ' Assign to object. obj = local ' Cast to direct interface. This works. Dim test2 As ILevel1 = CType(obj, ILevel1) test2.BaseTest() ' Cast to base interface. This throws InvalidCastException. Dim test3 As IBase = CType(obj, IBase) test3.BaseTest() End Sub End Module Public Interface IBase Sub BaseTest() End Interface Public Interface ILevel1 Inherits IBase End Interface Public Class Level1 Implements ILevel1 Public Sub BaseTest() Implements IBase.BaseTest Debug.Print("Base Test") End Sub Public Sub LocalTest() Debug.Print("Local Test") End Sub End Class End Namespace