D
D
Dmitry Gavrilenko2015-11-16 09:21:46
C++ / C#
Dmitry Gavrilenko, 2015-11-16 09:21:46

C# type conversion?

Hello. You need to convert the string to DateTime. Why do I need it? If a class with many properties. One such property is DateTime. I will convert the class instance to JSON manually. In JSON, the Date property (which in my class was of the DateTime type) is written by such a string received from DateTime.ToString(). After any changes with JSON data, I need to convert it back to DateTime. In principle, I can say that the body of the method will contain DateTim.Parse(str). How to do it exactly with the help of implicit and explicit?
PS: The reverse transformation of JSON -> MyType happens with new JavaScriptSerializer().Deserialize()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oxoron, 2015-11-16
@Oxoron

No way.
Operator overloading is only possible when the input or output type belongs to the class in which they are declared.
But you can make a wrapper. Here is an example about decimal. You don't need IXmlSerializer from there. You will overload operators both for a line, and for DateTime.
For DateTiee.Parse() docs and examples are here .
There will be something like

string format = "yyyyMMdd hh:mm:ss";
DateTime result = DateTime.Parse(arg, format);

M
MonkAlex, 2015-11-16
@MonkAlex

Or you can try newtonsoft.json , which will figure out how to overtake the date in both directions. To do this with your hands is to write a 100% bicycle with a rake.
PS: maybe the default JavaScriptSerializer can do it, I don't know.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question