一些代码来说明我的问题:
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