I have a list of types that I need to save to a file and read after it. I am using DataContractSerializer , but when deserializing I get an exception:
Unable to find constructor with arguments (SerializationInfo, StreamingContext) in ISerializable "System.RuntimeType".
I added System.RuntimeType as a known type for my serializer, but that didn't help.
Here is the code of my two methods
public static void SaveTypes(List<Type> types, string fileName) { Type rt = types[0].GetType(); List<Type> knownTypes = new List<Type>() { rt };
Serialization seems fine and the output file is fine, but the problem starts with deserialization:
public static object LoadTypes(string fileName) { Stream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); byte[] data = new byte[file.Length]; file.Read(data, 0, (int)file.Length); Type rt = file.GetType(); List<Type> knownTypes = new List<Type>() { rt.GetType() }; DataContractSerializer deserializer = new DataContractSerializer(typeof(List<Type>), knownTypes); Stream stream = new MemoryStream(); stream.Write(data, 0, data.Length); stream.Position = 0; return deserializer.ReadObject(stream);
Is there any way to get through this? Or maybe there is another way to store types?
source share