C# 4.0 'dynamic' doesn't set ref/out arguments

后端 未结 3 1184
灰色年华
灰色年华 2020-12-03 17:51

I\'m experimenting with DynamicObject. One of the things I try to do is setting the values of ref/out arguments, as shown in the code

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 18:48

    This looks like it could be a bug - probably in DynamicObject. If you add a Wrap method to Proxy like this:

    public void Wrap(ref int x, ref int y)
    {
        target.Swap(ref x, ref y);
    }
    

    Then even though this is still called dynamically (i.e. the code in Main stays the same) the code works... so at least the general "how does a dynamic object work" layer supports pass-by-reference.

    I suspect if this is indeed a bug in the DLR, it may be too late to fix for .NET 4 - but it's worth reporting on Connect anyway so it can be fixed in a service pack. Alternatively, if this is a deliberate restriction/limitation, it should be clearly documented in MSDN (which it isn't at the moment, as far as I can see).

提交回复
热议问题