I could easily write one, but I’m wondering if there is already a standard string deserialization interface or pattern somewhere in the framework that creates/populates an object based on a string.
Basically I’m looking to do the opposite of ToString(). Every object supports ToString(), which is handy, but it would be nice if I could go the other way, and actually (re)create an object from that string. Obviously this isn’t possible for all objects, but that’s fine.
I looked at ISerializable but it’s silly complicated (having SerializationInfos and StreamingContexts and all sorts of nonsense) for something that should be a core feature, imo. I’m envisioning something like this:
public interface IDeserializable {
void Deserialize( string data );
}
Upon further thought, it would be nice (and be more symmetric to ToString) if I could actually pass the string to a constructor, but I wouldn’t be able to use an interface to represent this. I’d have to use reflection on the type and check for a constructor that accepts a single string, and use reflection to instantiate it.
As a reflection/Type- friendly alternative to TryParse: