[英]Deserialize List<Object> C#
I want to assign the List<Object>
I recieve from this method to a new List<MyClass>
我想将我从此方法接收的
List<Object>
分配给新的List<MyClass>
public List<Object> Deserialize(string path)
{
IFormatter seri = new BinaryFormatter();
Stream str = new FileStream(path, FileMode.Open, FileAccess.Read);
List<Object> Lista = (List<Object>)(seri.Deserialize(str));
str.Close();
return Lista;
}
My intention is to call this method with different Classes and then cast it back to the previous type, but I get an error trying to cast it back afterwards. 我的意图是使用不同的类调用此方法,然后将其强制转换回以前的类型,但是在尝试将其强制转换后出现错误。
You can try using Generics
: 您可以尝试使用
Generics
:
public List<T> Deserialize<T>(string path)
{
IFormatter seri = new BinaryFormatter();
Stream str = new FileStream(path, FileMode.Open, FileAccess.Read);
List<T> Lista = (List<T>)(seri.Deserialize(str));
str.Close();
return Lista;
}
public List<T> Deserialize<T>(string path)
{
return JsonConvert.DeserializeObject<List<T>>(path);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.