方法调用后方法参数的值发生变化 [英] The value of method argument changes after the method is called

查看:82
本文介绍了方法调用后方法参数的值发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是我太累了,出现了幻觉,或者 C# + Mono 真的有问题!!

Perhaps I'm too tired and I'm hallucinating or maybe there's actually something wrong with C# + Mono!!

当我遇到 SetPropertyValue 方法时,我正在 Json.Net 中进行测试和调试JsonSerializerInternalReader 类的.此方法调用另一个名为 CalculatePropertyDetails 的私有方法.传递给CalculatePropertyDetails 的参数之一称为reader,类型为JsonReader.此对象在传递给 CalculatePropertyDetails 方法之前已获得值,之后为空!!

I was testing and debugging in Json.Net while I came across SetPropertyValue method of class JsonSerializerInternalReader. This method calls another private method named CalculatePropertyDetails. One of the arguments passed to CalculatePropertyDetails is called reader of type JsonReader. This object has got value before it is passed to the CalculatePropertyDetails method and it's null afterwards!!

我知道这听起来很愚蠢,相信我我并不天真,但是为了看到这种奇怪的行为会发生什么?谁能给出合理的解释?我只知道这个项目不是多线程的,结果是可重现的.

I know it sounds so stupid and believe me I'm not naive, but what could have happened in order to see such strange behavior? Can anyone come up with a rational explanation? All I know is that this project is not multi-threaded and the results are reproducible.

我得到的唯一证据是调用堆栈顶部的两个条目:

The only proof I've got is the two entries on top of my call stack:

Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CalculatePropertyDetails (property={Id}, propertyConverter={JsonRpcTest.ObjectIdConverter}, containerContract={Newtonsoft.Json.Serialization.JsonObjectContract}, containerProperty=(null), reader=(null), target=(null), useExistingValue=false, currentValue=(null), propertyContract=(null), gottenCurrentValue=false) in Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:792
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (property={Id}, propertyConverter={JsonRpcTest.ObjectIdConverter}, containerContract={Newtonsoft.Json.Serialization.JsonObjectContract}, containerProperty=(null), reader={Newtonsoft.Json.JsonTextReader}, target={JsonRpcTest.E}) in Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:750

寻找 reader 参数.

我使用的是 Ubuntu 12.04、Mono 3.2.3 和 Monodevelop 4.1.7,项目是使用 Mono/.Net 4.5 开发的.

I'm using Ubuntu 12.04, Mono 3.2.3 and Monodevelop 4.1.7 and project is developed using Mono / .Net 4.5.

[更新]

以下是定义 SetPropertyValue 和调用 CalculatePropertyDetails 的部分代码:

Here's a part of code from where SetPropertyValue is defined and where CalculatePropertyDetails is called:

private bool SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, object target)
{
    object currentValue;
    bool useExistingValue;
    JsonContract propertyContract;
    bool gottenCurrentValue;

    if (CalculatePropertyDetails(property, ref propertyConverter, containerContract, containerProperty, reader, target, out useExistingValue, out currentValue, out propertyContract, out gottenCurrentValue))
        return false;

推荐答案

可能是GC在执行时收集你的对象?我在其中一个单音叉中看到了这种行为.您可以检查它 - 简单地在 SetPropertyValue 的父类中使用 reader 对象创建公共字段,并使用实际读取器设置它.在 GC 问题的情况下,此后不应将其归零.

May be GC collects your object while execution? I saw such behaviour in one of the mono forks. You can check it - simple create the public field with reader object in the parent class for SetPropertyValue and set it with actual reader. In case of GC issue it should not be nulled after this.

这篇关于方法调用后方法参数的值发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆