TypeDescriptor.GetConverter(Type) is a very convientent way to serialize/deserialize lots of built-in data types to/from strings:
object original = ...;
string serialized = TypeDescriptor.GetConverter(t).ConvertToInvariantString(original);
object deserialized = TypeDescriptor.GetConverter(t).ConvertFromInvariantString(serialized);
Unfortunately, TypeDescriptor is not available in portable class libraries.
Is there a canonical replacement or do we have to go back to huge switch statements?
You can not use
TypeDescriptorin PCL projects because it is not built as a PCL and it is not cross platform. TheTypeDescriptoris unavailable in PCL projects and it is not listed here.The following assemblies are available within a Portable Class Library project: