Set DataContract and DataMember Without All the Attributes

后端 未结 3 918
死守一世寂寞
死守一世寂寞 2021-02-18 18:23

I find the [DataContract] and [DataMember] attributes a bit messy and would rather do this with code in a config method or something. Is this possible?

3条回答
  •  野性不改
    2021-02-18 18:56

    I know this is a rather old post, but I came here thinking the same thing if there is a way to set all member attributes automatically on some legacy code with public fields and no getters and setters.

    What makes it look just a little bit less messy is shortening up the name DataMember:

    using DM = System.Runtime.Serialization.DataMemberAttribute;
    
    [DataContract]
    public class SomeClass
    {
        [DM] public bool IsMO;
        [DM] public string LabCode;
        [DM] public string OrderNumber;  
    }
    

提交回复
热议问题