调用方法后,方法参数的值会更改

本文关键字:方法 参数 调用 | 更新日期: 2023-09-27 18:37:11

也许我太累了,出现了幻觉,或者C# + Mono实际上有问题!!

我正在 Json.Net 进行测试和调试,同时遇到了SetPropertyValueJsonSerializerInternalReader的方法。此方法调用另一个名为 CalculatePropertyDetails 的私有方法。传递给CalculatePropertyDetails的参数之一称为类型 JsonReaderreader。这个对象在传递给 CalculatePropertyDetails 方法之前已经有了值,之后它是 null!!

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

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

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 和 Monodevelopment 4.1.7,项目是使用 Mono / .Net 4.5 开发的。

[更新]

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

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 的父类中使用读取器对象创建公共字段,并使用实际读取器对其进行设置。如果出现 GC 问题,则在此之后不应将其取消。