Tôi có một Danh sách các loại mà tôi cần lưu vào tệp và đọc sau. tôi sử dụng DataContractSerializer
nhưng tôi nhận được một ngoại lệ trong deserialization:Serialize/deserialize System.RuntimeType
Không thể tìm thấy nhà xây dựng với các đối số (SerializationInfo, StreamingContext) trong ISerializable "System.RuntimeType".
Tôi đã thêm System.RuntimeType
như một loại biết đến serializer của tôi, nhưng nó đã không giúp đỡ.
Đây là mã của hai phương pháp của tôi
public static void SaveTypes(List<Type> types, string fileName)
{
Type rt = types[0].GetType();
List<Type> knownTypes = new List<Type>() { rt }; //I get a List with System.RuntimeType item here
DataContractSerializer serializer = new DataContractSerializer(typeof(List<Type>), knownTypes);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
Stream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
using (XmlWriter xw = XmlWriter.Create(fs, settings))
serializer.WriteObject(xw, types);
}
serialization dường như làm việc tốt, và các tập tin đầu ra là ok, nhưng vấn đề bắt đầu từ ngày deserializing:
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); //exception here
}
Có cách nào để đi qua điều này? Hoặc có thể có một số cách khác để lưu trữ các loại?
Bạn nên tuần tự hóa ** dữ liệu **, chứ không phải triển khai. 'Loại' là 100% về việc triển khai. Thành thật mà nói, nó chỉ đơn giản là sai đường để đi, và nó sẽ là một sự bất hòa để giúp bạn đi xa hơn con đường tối đó. IMO. –
Tôi làm tuần tự hóa dữ liệu, nhưng nó chứa các loại do người dùng xác định mà tôi không thể truy cập. Vì vậy, khi tôi deserialize chúng tôi cần phải biết rằng các loại. Tôi nghĩ cách duy nhất để có được quyền truy cập vào chúng là lưu trữ chúng trong một tệp. –