Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

c# - Is there a good way to create general property name can support multi properties?

I need to create a data class which used for JsonConvert. Most of the resource string which used to be converted are same except the following properties:

public class DataType
{
        public DateTimeOffset CreateDate { get; set; }
        public DateTimeOffset Create_Date { get; set; }
}

Because in the resource string there are 2 types : "create_date": "2021-01-15T18:43:13.061+0000", & "createdate": "2021-01-15T18:43:13.061+0000",

with the following JsonConvert:

JsonConvert.DeserializeObject<DataType>(resourceStr);

The output of one of the datetime property will be wrong value. Is there a good way to use one property to handle both of these two formats?

question from:https://stackoverflow.com/questions/65911330/is-there-a-good-way-to-create-general-property-name-can-support-multi-properties

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's resolved by add another private property with alter name. Then the class will support to convert both CreateDate and Create_Date with a single display name;

public DateTimeOffset CreateDate { get; set; }

[JsonProperty("Create_Date")]
        private DateTimeOffset CreateDate2
        {
            set => CreateDate = value;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...