Joe*_*gan 5 vb.net interface instantiation
我正在查看我们的一些遗留代码,我注意到一些令我印象深刻的东西.
假设我们有具体的课程TestClass
. TestClass
实现接口ITestClass
.
那么在下列情况下我应该期待什么样的行为呢?(我没有意识到这甚至可能)
Dim testClass as TestClass = Nothing
Try
testClass = New ITestClass
...
End Try
Run Code Online (Sandbox Code Playgroud)
据我所知,你会被迫使用TestClass
而不是它的界面对应物.
有一种特殊情况,其中接口可以像类一样实例化,并且它与CoClassAttribute相关.有关详细信息,请参阅此博文:
从博客文章转换为VB的示例:
<ComImport(), Guid("C906C002-B214-40d7-8941-F223868B39A5"), CoClass(GetType(Foo))> _
Public Interface IFoo
End Interface
Public Class Foo
Implements IFoo
End Class
Sub Main()
Dim f As New IFoo() ' Compiles
End Sub
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
333 次 |
最近记录: |