Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse a .ToString() call

Tags:

c#

.net

Each object in C# has the potential to be streamed to a string as ToString(). Is there a one liner that would reverse the process given the string originally produced?

So:

var stringSomeObject = SomeObject.ToString();

and then to reverse:

var someObjectFromToString = ??????.FromString(stringSomeObject);
like image 596
TheEdge Avatar asked Nov 24 '25 06:11

TheEdge


1 Answers

Is there a one liner that would reverse the process given the string originally produced?

No. Every type has the option of implementing ToString() in any way is desires. This makes it impossible to arbitrarily round trip the value.

Round tripping objects to and from some other representation is typically handled via the framework's support for Object Serialization.

Specific types within the Framework do implement methods following specific patterns which handle this, however. Many of the default value types (Int32, Double, etc) implement a .Parse and .TryParse method which can be used to "round trip" from a string. This is something that has to be implemented per type, however, and does not work on arbitrary types.

like image 77
Reed Copsey Avatar answered Nov 25 '25 21:11

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!