Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,7 @@ private void WriteStructMethod(StructMapping mapping, string n, string? ns, obje

if (m.CheckShouldPersist)
{
string methodInvoke = $"ShouldSerialize{m.Name}";
MethodInfo method = o!.GetType().GetMethod(methodInvoke, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)!;
shouldPersist = (bool)method.Invoke(o, Array.Empty<object>())!;
shouldPersist = (bool)m.CheckShouldPersistMethodInfo!.Invoke(o, Array.Empty<object>())!;
}

if (m.Attribute != null)
Expand Down Expand Up @@ -693,9 +691,7 @@ private void WriteStructMethod(StructMapping mapping, string n, string? ns, obje

if (m.CheckShouldPersist)
{
string methodInvoke = $"ShouldSerialize{m.Name}";
MethodInfo method = o!.GetType().GetMethod(methodInvoke, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)!;
shouldPersist = (bool)method.Invoke(o, Array.Empty<object>())!;
shouldPersist = (bool)m.CheckShouldPersistMethodInfo!.Invoke(o, Array.Empty<object>())!;
}

bool checkShouldPersist = m.CheckShouldPersist && (m.Elements!.Length > 0 || m.Text != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,30 @@ public static void Xml_TypeWithShouldSerializeMethod_WithNonDefaultValue()
Assert.Equal(value.Foo, actual.Foo);
}

[Fact]
public static void Xml_InheritedShouldSerializeMethod_WithDefaultValue()
{
var value = new DerivedTypeWithInheritedShouldSerialize();

var actual = SerializeAndDeserialize(value, WithXmlHeader("<DerivedTypeWithInheritedShouldSerialize xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" />"));

Assert.NotNull(actual);
Assert.Equal(value.Foo, actual.Foo);
Assert.Equal(value.Bar, actual.Bar);
}

[Fact]
public static void Xml_InheritedShouldSerializeMethod_WithNonDefaultValue()
{
var value = new DerivedTypeWithInheritedShouldSerialize() { Foo = "SomeValue", Bar = "SomeBar" };

var actual = SerializeAndDeserialize(value, WithXmlHeader("<DerivedTypeWithInheritedShouldSerialize Bar=\"SomeBar\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Foo>SomeValue</Foo></DerivedTypeWithInheritedShouldSerialize>"));

Assert.NotNull(actual);
Assert.Equal(value.Foo, actual.Foo);
Assert.Equal(value.Bar, actual.Bar);
}

[Fact]
public static void Xml_KnownTypesThroughConstructorWithArrayProperties()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,28 @@ public bool ShouldSerializeFoo()
}
}

public class BaseTypeWithShouldSerializeMethod
{
public string Foo { get; set; } = "default";

[System.Xml.Serialization.XmlAttribute]
public string Bar { get; set; } = "default";

public bool ShouldSerializeFoo()
{
return Foo != "default";
}

public bool ShouldSerializeBar()
{
return Bar != "default";
}
}

public class DerivedTypeWithInheritedShouldSerialize : BaseTypeWithShouldSerializeMethod
{
}

public class KnownTypesThroughConstructorWithArrayProperties
{
public object StringArrayValue;
Expand Down
Loading