vb.net – 如何访问对象本身与…结束与

一些代码来说明我的问题:
With Test.AnObject

    .something = 1337
    .AnotherThing = "Hello"

    ''// why can't I do this to pass the object itself:
    Test2.Subroutine(.)
    ''// ... and is there an equivalent,other than repeating the object in With?

End With
没有办法引用With语句中引用的对象,而不是重复对象本身的名称.

编辑

如果你真的想,你可以修改你的AnObject来返回一个引用自己

Public Readonly Property Self as TypeOfAnObject
    Get
        Return Me
    End Get
End Get

那么你可以使用下面的代码

With Test.AnObject
    Test2.Subroutine(.Self)
End With

最后,如果您无法修改AnObject的代码,您可以(但不一定应该)通过extension method完成相同的操作.一个通用的解决方案是:

' Define in a Module
<Extension()>
Public Function Self(Of T)(target As T) As T
    Return target
End Function

像这样叫:

Test2.Subroutine(.Self())

要么

With 1
   a = .Self() + 2 ' a Now equals 3
End With

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...