I want to deserialize to a type, but I only have the string representation of that type.
All I know is that the type implements ISomething
.
string typeName = "MyClass";
BinaryFormatter binaryFormatter = new BinaryFormatter();
byte[] data = Convert.FromBase64String(serialisedString);
using (MemoryStream memoryStream = new MemoryStream(data, 0, data.Length))
{
return (ISomething)binaryFormatter.Deserialize(memoryStream) as ISomething;
}
But I get the following exception on BinaryFormatter.Deserialize:
Unable to cast object of type 'System.RuntimeType' to type 'MyAssembly.ISomething'
How do I cast to the class name stored in typeName
?