开发者

Using Extension Methods from within an Object's constructor where "Me" is the ByRef target object

Consider the following:

Public Module Extensions
    <Extension()> _
    Public Sub Initialize(ByRef Target as SomeClass, ByVal SomeParam as Something )
        ...
        Target  = SomethingElse
    end Sub
End Module

Class SomeClass
...

    sub New(ByVal SomeParam as Something )
       Me.Initialize(SomeParam)
    end sub

    sub New()

    end sub

End Class


'Case 1: Doesnt Work...why????:

    Dim foo as new SomeClass(SomeParam) 'foo rem开发者_运维问答ains uninitialized


'Case 2: Does Work:
    Dim foo as new SomeClass()
    foo.Initialize(SomeParam) 'foo is initialized

Question: Why is Case 1 failing to initialize the object as expected?


The problem here is that VB.Net supports multiple ways of using ByRef parameters. I did a detailed explanation of the types in a recent blog entry

  • http://blogs.msdn.com/jaredpar/archive/2010/01/21/the-many-cases-of-byref.aspx

What's happening here is that Me is not an assignable value. So instead of passing Me as a byRef parameter, the VB compiler will instead pass a temporary. This is loosely known as "Copy Back ByRef". It effectively generates the following code

Dim temp = Me
Initialize(temp, SomeParam)

There is no way to work around this in the case of Me because it's not assignable. In the case of the local variable foo though this works as expected because foo is a valid ByRef value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜