Vb.net 意外的接口转换

Vb.net 意外的接口转换,vb.net,Vb.net,我只是偶然发现,这似乎是可行的: Public Interface Ix ReadOnly Property TestProp() End Interface Public Interface Iy Property TestProp() End Interface Public Sub TestSub Dim x As Ix = Me.InstantiationNotImportant() Dim y As Iy = CType(x, Iy) End Sub

我只是偶然发现,这似乎是可行的:

Public Interface Ix
    ReadOnly Property TestProp()
End Interface

Public Interface Iy
    Property TestProp()
End Interface

Public Sub TestSub
    Dim x As Ix = Me.InstantiationNotImportant()
    Dim y As Iy = CType(x, Iy)
End Sub
也许我今天写了太多的代码,但这对我来说没有意义。我怎么可能将一个接口转换成另一个甚至略有不同的接口

有什么想法吗


编辑:看来我确实是因为太多的编码而头晕目眩。睡了一会儿,世界又变得有意义了

实例化很重要,因为该方法可以生成一个实现两个接口的对象,从而使该代码完全有意义。否则,我不确定。

就像Garry说的实例化很重要,因为如果我这样做:

public interface IX { }
public interface IY { }
public class Test : IX { }
下面的方法行不通

IX xvar = new Test();
IY yvar = xvar as IY; \\Returns null.

IY yvar = (IY)xvar; \\Throws cast exception.
它会运行,但yvar将为null,因为强制转换是不可能的,但如果您这样声明测试:

public class Test : IX,IY { }
它现在可以工作了,因为测试使用了两个接口,并且从xvar中的对象到Y的转换是有效的