TheEdge
TheEdge

Reputation: 9861

How to reverse a .ToString() call

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);

Upvotes: 3

Views: 5689

Answers (4)

Myke Black
Myke Black

Reputation: 1339

you can create a generic method like this:

public static T FromString<T>(string value)
{
    var result = default(T);
    if (value != null)
    {
        try {result = (T)Convert.ChangeType(value, typeof(T));  } catch {}
    }
    return result;
}

and use it like this:

string numberstring = "23";   
int myNumber = FromString<Int32>(numberstring);
// myNumber is now 23

numberstring = "notanumber";   
myNumber = FromString<Int32>(numberstring);
// myNumber is now 0

string decimalstring = "3.14159";
int myDecimal = FromString<Decimal>(numberstring);
// myDecimal is now 3.14159

so if the string cannot be converted, then the default value for the type will be returned. default values for each type can be found here: Default values table (C# Reference)

It would be nice is MS could add int.FromString(string) method as part of the .net framework, but if you want, you could add your own extension methods to each data type (probably not worth the effort though)

Upvotes: 0

Ethan Li
Ethan Li

Reputation: 1001

You actually want serialise and dserialise object.

Google 'c# serialise', and you will find a lots of thing that you can use.

Such as xmlserialiser, datacontractserialiser, jsonserialiser, or binaryformator

I think the jsonserialiser is what you want, which serialise object to json string,

And you can deserialise the json string back to object

Upvotes: 1

SecurityMatt
SecurityMatt

Reputation: 6743

No. ToString() is not a bijection with object - that is to say two entirely different objects can return the same string when ToString() is called on them.

For example, the value "1" and the number 1 and any type I've implemented to return the value "1" all return the string value "1".

Any value that is returned by such a method ?????.FromString() is therefore incorrect, because if I call ????.FromString("1") and it returns the number 1, I can complain that it didn't return my class that overloads ToString. Similarly, if it returns my type, I can complain that it didn't return the integer value 1.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564481

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.

Upvotes: 17

Related Questions