加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:10:47 所属栏目:大数据 来源:网络整理
导读:一些代码来说明我的问题: 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 W
一些代码来说明我的问题:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读