You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I tried to use anchor to simplify the repetitive content in my yaml configuration. I thought anchor just represented repeated yaml text content, but I found that the objects represented by anchor all refer to the same object, rather than repeatedly creating different (reference) objects as I imagined.
If the deserialized data is read-only, there should be no problem, but occasionally I want to store some states in the deserialized object. At this time, some conflicts will occur due to they are actually the same reference object.
I want to know if this behavior is designed? Is my understanding of anchor wrong?
Thank you very much for your answer and help. ❤
To Reproduce
usingYamlDotNet.Serialization;usingYamlDotNet.Serialization.NamingConventions;namespaceYamlAnchorTest;publicclassConfiguration{publicList<Executor>Executors{get;set;}}publicclassExecutor{publicstringName{get;set;}publicList<Filter>Filters{get;set;}}publicclassFilter{publicstringType{get;set;}publicintValue{get;set;}}internalstaticclassProgram{privatestaticvoidMain(string[]args){vardocument=""" executors: - name: a filters: &shared - type: foo value: 1 - type: bar value: 2 - name: b filters: *shared """;vardeserializer=newDeserializerBuilder().WithNamingConvention(HyphenatedNamingConvention.Instance).WithEnumNamingConvention(HyphenatedNamingConvention.Instance).Build();varconf=deserializer.Deserialize<Configuration>(document);Console.WriteLine(ReferenceEquals(conf.Executors[0].Filters,conf.Executors[1].Filters));// Output: true// If I add an item to the Filters of one Executor at this time, the other Executor will also changed}}
The text was updated successfully, but these errors were encountered:
Describe the bug
I tried to use anchor to simplify the repetitive content in my yaml configuration. I thought anchor just represented repeated yaml text content, but I found that the objects represented by anchor all refer to the same object, rather than repeatedly creating different (reference) objects as I imagined.
If the deserialized data is read-only, there should be no problem, but occasionally I want to store some states in the deserialized object. At this time, some conflicts will occur due to they are actually the same reference object.
I want to know if this behavior is designed? Is my understanding of anchor wrong?
Thank you very much for your answer and help. ❤
To Reproduce
The text was updated successfully, but these errors were encountered: