如何将可序列化对象保存到文件或从文件恢复可序列化对象? (第二部分)动态对象

发布于 2024-11-09 20:10:32 字数 1141 浏览 0 评论 0原文

我之前问过这个问题 如何将可序列化对象保存/恢复到/从文件? 当我知道我将保存什么类型的对象时,该解决方案非常有效。

如果我可以保存一个未知类型的对象,那就太好了。

无论如何,我想做类似的事情:

[Serializable]
MyCustomClass
{
   // properties. I don't know which properties this class has. I might pass a different class. 
}


public void saveObjectInComputer(object obj, string pathWhereToSaveObject)
{
     // code
}

 public object deserializeObject(string pathWhereObjectIsLocated)
 {
      // code to retrive object
      // ...

      return object;
 }

static void Main(string[] args)
{
     List<MyCustomClass> myList = new List<MyCustomClass>();
     myList.add(new MyCustomClass { "properties go here" } );

     saveObjectInComputer(myList, @"C:\Temp\object.txt");

     // code  

     // Later it will be nice if I could retrive the object 

     object a = deserializeObject(@"C:\Temp\object.txt");

     // then cast a to List<MyCustomClass>

}

稍后,如果我构建一个汽车列表或一个人列表,我可以使用相同的方法,而不是为每个构建一个新方法。此外,后来我知道我正在寻找的方法将返回一个对象,并且我可以将该对象转换为我当前正在使用的类型。

I previously asked the question How to save/restore serializable object to/from file? that solution works great when I know what type of object I will be saving.

It will be nice if I could save an object of unknown type.

Anyways I am looking to do something like:

[Serializable]
MyCustomClass
{
   // properties. I don't know which properties this class has. I might pass a different class. 
}


public void saveObjectInComputer(object obj, string pathWhereToSaveObject)
{
     // code
}

 public object deserializeObject(string pathWhereObjectIsLocated)
 {
      // code to retrive object
      // ...

      return object;
 }

static void Main(string[] args)
{
     List<MyCustomClass> myList = new List<MyCustomClass>();
     myList.add(new MyCustomClass { "properties go here" } );

     saveObjectInComputer(myList, @"C:\Temp\object.txt");

     // code  

     // Later it will be nice if I could retrive the object 

     object a = deserializeObject(@"C:\Temp\object.txt");

     // then cast a to List<MyCustomClass>

}

And later if I construct a list of cars or a list of people I could use the same method instead of constructing a new method for each. Moreover later I know that the method I am looking for will return an object and I could cast that object to the type that I am currently working with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-11-16 20:10:32

您无法轻松序列化未标记 Serialized 属性的对象,因此简短的答案是否定的。

如果您愿意进入中等难度的代码,您可以使用反射并设计一种自定义格式来保存状态,使用索引属性等的异常。不过,您也可能不这样做。

You cannot serialize an object that has not been marked with the Serializable attribute easily, so the short answer is no.

If you're willing to step into moderately difficult code, you could use reflection and devise a custom format to save states with, using exceptions for things like indexed properties, etc.. You might as well just not, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文