Skip to content

Commit

Permalink
EnumMemberAttribute #20
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Sep 12, 2018
1 parent 65b4a4c commit 301925f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class SerializeWithDataContractAttributes
new TestCaseData(new FieldWithIgnoreDataMemberAttribute { Value = 1 }),
new TestCaseData(new FieldWithDataMemberAttribute { Value = 1 }),
new TestCaseData(new FieldWithDataMemberAttributeExplicitName { Value = 1 }),
new TestCaseData(new WithWithEnumMemberAttribute { Value = WithEnumMemberAttribute.One }),
new TestCaseData(new WithWithEnumMemberAttribute { Value = WithEnumMemberAttribute.Two }),
};

[TestCaseSource(nameof(Values))]
Expand Down Expand Up @@ -117,6 +119,20 @@ public class FieldWithIgnoreDataMemberAttribute
[IgnoreDataMember]
public int Value { get; set; }
}

public class WithWithEnumMemberAttribute
{
public WithEnumMemberAttribute Value { get; set; }
}

[DataContract]
public enum WithEnumMemberAttribute
{
[EnumMember(Value = "Single")]
One,
[EnumMember(Value = "Double")]
Two,
}
}
}
}
3 changes: 2 additions & 1 deletion Gu.Xml/Writers/EnumWriter{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private string ToString(T value)
if (typeof(T).GetMember(value.ToString()).TryFirst(out var member))
{
if (TryGetNameFromAttribute<System.Xml.Serialization.XmlEnumAttribute>(member, x => x.Name, out var name) ||
TryGetNameFromAttribute<System.Xml.Serialization.SoapEnumAttribute>(member, x => x.Name, out name))
TryGetNameFromAttribute<System.Xml.Serialization.SoapEnumAttribute>(member, x => x.Name, out name) ||
TryGetNameFromAttribute<System.Runtime.Serialization.EnumMemberAttribute>(member, x => x.Value, out name))
{
return name;
}
Expand Down

0 comments on commit 301925f

Please sign in to comment.