Is it possible to add an attribute to a property in a partial class?

前端 未结 4 568
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 00:53

I don\'t think it\'s possible but since I haven\'t got a definite clarity from MSDN, I feel that it\'s best to ask. Suppose that we have a class as follows.

         


        
4条回答
  •  执念已碎
    2020-12-07 01:34

    The best we could to to ensure decimals serialized to 2 decimal places was tp:

    1. Use XlmAttributeOverides to ignore the decimal filed i.e not serialze it

    var xmlAttributeOverrides = new XmlAttributeOverrides(); var ctrlSumAttributes = new XmlAttributes { XmlIgnore = true }; xmlAttributeOverrides.Add(typeof(Sepa.GroupHeader39), "CtrlSum", ctrlSumAttributes); var ser = new XmlSerializer( typeof( Sepa.Document ), xmlAttributeOverrides);

    1. Use a partial class to add a string serialization of the ignored field

    public partial class GroupHeader39 { [XmlElement("CtrlSum")] public string CtrlSumString { get => CtrlSum.ToString("F2"); set { /* required to work */ } } }

    I hope this helps someone out with this issue.

提交回复
热议问题