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
Is it possible to serialize private properties as well? In the end I want to serialize all properties that have the YamlMember. I found this question in variations multiple times on Stackoverflow without any answers.
Example:
I want to have both t1 and t2 serialized:
using System;
using System.IO;
using YamlDotNet.Serialization;
class Foo
{
public Foo() {}
public Foo(int t1, int t2)
{
this.T1 = t1;
this.T2 = t2;
}
[YamlMember(Alias = "t1")]
private int T1 { get; set; }
[YamlMember(Alias = "t2")]
public int T2 { get; private set; }
public void Print()
{
Console.WriteLine($"{this.T1} - {this.T2}");
}
}
public class Program
{
public static void Main()
{
var serializer = new SerializerBuilder().Build();
var deserializer = new DeserializerBuilder().Build();
var foo = new Foo(1, 2);
var strWriter = new StringWriter();
serializer.Serialize(strWriter, foo);
// t1 has not been serialized
Console.WriteLine(strWriter.ToString());
var str = @"
t1: 3
t2: 4
";
// Fails, "can't find t1"
var foo2 = (Foo)deserializer.Deserialize(str, typeof(Foo));
foo2.Print();
}
}
The text was updated successfully, but these errors were encountered:
Unfortunately it is not possible at this time. Doing so would require to replace a component that is hard-coded, but I will look into fixing that.
Also, your use case seems to make a lot of sense and should probably be supported out of the box.
I believe this is possible now. Pretty sure there’s something like includeprivatemembers on the serializerbuilder.
Closing since the solution exists and no comments in almost 4 years.
Is it possible to serialize private properties as well? In the end I want to serialize all properties that have the
YamlMember
. I found this question in variations multiple times on Stackoverflow without any answers.Example:
I want to have both
t1
andt2
serialized:The text was updated successfully, but these errors were encountered: