Giter Club home page Giter Club logo

Comments (1)

exodrifter avatar exodrifter commented on August 11, 2024

Response received from Unity:

Thanks for submitting this issue. I've looked into the code example you've sent and the issue appears to be in your deserialization constructor. It appears that the code worked in our old 3.5 runtime purely out of luck, but items you pull from the SerializationInfo aren't guaranteed to have been deserialized before the end of the constructor.

public Scope(SerializationInfo info, StreamingContext context)
{
    var keys = info.GetValue<List<string>>("keys");
    var values = info.GetValue<List<Value>>("values");
    vars = new Dictionary<string, Value>();
    for (int i = 0; i < keys.Count; ++i)
    {
        vars.Add(keys[i], values[i]);
    }
    DefaultSpeaker = info.GetValue<object>("defaultSpeaker");
}

This code looks reasonable to me..you're using two lists to serialize a dictionary's keys and values, and then recreating the dictionary... but after discussing with other members of the .net community from mono and .netcore, it would appear that when you get the values and store it in the "values" the items are not guaranteed to be deserialized yet because we are still in the deserialization constructor of of Scope. This means we may (or may not) be adding null keys and values to the dictionary here. To manipulate the data in this way, you'll need to do this after deserialization is completed. This can be done by marking a function with OnDeserializedAttribute or by setting a DeserializationCallback.OnDeserialization.

Given your sample, I was able to get the test to pass making these changes. Maybe there is a more elegant way, but I added lists for the keys and values to the Scope, and then deserialized them with the deserialization constructor, and then recreated the dictionary with the OnDeserialized function.

private Dictionary<string, Value> vars;
//For serialization only
private List<string> _keys;
private List<Value> _values;

public Scope(SerializationInfo info, StreamingContext context)
{
    _keys = info.GetValue<List<string>>("keys");
    _values = info.GetValue<List<Value>>("values");
    DefaultSpeaker = info.GetValue<object>("defaultSpeaker");
}

[OnDeserialized]
public void repopulateVars()
{
    vars = new Dictionary<string, Value>();
    for (int i = 0; i < _keys.Count; ++i)
    {
        vars.Add(_keys[i], _values[i]);
    }
}

Again, Thank you so much for submitting this issue! even in cases where the new behavior is considered "correct" Its good that we're of these behavior differences.

-Michael DeRoy

from unity-rumor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.