在C#中读取/写入对象ro文件 [英] Read/Write objects ro file in C#

查看:81
本文介绍了在C#中读取/写入对象ro文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做这样的事情很容易

It''s easy to do something like this

foreach(Grade curr in arrList)
{
     myStreamWriter.Write(Grade.Name);
     myStreamWriter.Write(Grade.Score);
}


有一种简单的方法可以读取/写入所有成绩对象吗?

编辑
====

当然,该示例应该是curr.property
但是我正在寻找一种一次性写所有curr的方法.


Is there an easy method to read/write all the Grade object?

Edit
====

Of course the example should have been curr.property
but I am looking for a way to write all the curr at once

推荐答案

您的尝试是一种幼稚的方法.最好,最通用,最简单和最不介入的方法是使用数据协定.

请参阅 http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

另请参阅我过去的答案,在其中我提倡使用这种出色的方法并解释一些重要的细节:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [反序列化json字符串数组 [
Your attempt was a naive way. The best, most universal, easiest and non-intrusive way is using Data Contract.

Please see http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers where I advocate this wonderful approach and explain some important detail:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


尝试使用不同的对象序列化技术.这是MSDN文章列表.
1.
DataContract序列化器 [如何使用Visual C#将对象序列化为XML [演练:持久化对象 [
Try to use different object serialization technique. Here are lists of MSDN articles.
1. DataContract Serializer [^]
2. How to serialize an object to XML by using Visual C#[^]
3. Walkthrough: Persisting an Object[^]


Easy是一个相对术语.你想达到什么目的?只需您的代码,即可提高性能?

您可以确保对象是可序列化的,然后将它们序列化为某个文件流,但是您拥有的方法很简单.
Easy is a relative term. What are you trying to accomplish? Simply your code, improve performance?

You could make sure the objects are serializable then serialize them to some file stream, but the method you have is simple enough.


这篇关于在C#中读取/写入对象ro文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆