为什么没有序列化

houqidian 2010-06-08 10:50:30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
namespace User
{
[Serializable ]
public class UserInformation
{
private string UserName;
private string UserPassWord;

public UserInformation() { }
public UserInformation(string name, string password)
{
UserName = name;
UserPassWord = password;
}

public override string ToString()
{
return string.Format("Name:{0},PassWord:{1}", UserName, UserPassWord);
}
}

[Serializable]//使之能够序列化
public class UserCollection
{
private ArrayList UserList = new ArrayList();

public UserCollection() { }

public UserInformation GetUser(int pos)
{
return (UserInformation )UserList[pos];
}

public void AddUser(UserInformation p)
{
UserList.Add(p);
}

public void ClearUser()
{
UserList.Clear();
}

public int GetCount()
{
return UserList.Count;
}

}
}










using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using User;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization ;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string name, password;
//UserInformation a=new UserInformation ();
UserCollection b=new UserCollection ();

FileInfo f = new FileInfo(@"D:\Test.dat");
FileStream fs = f.Create();
fs.Close();

for (int i = 0; i < 2; i++)
{
name = Console.ReadLine();
password = Console.ReadLine();
b.AddUser(new UserInformation(name, password));
}
int count = b.GetCount();
Console.WriteLine(count);
for (int j = 0; j < count; j++)
{
Console.WriteLine(b.GetUser(j));
}
SaveAsBinaryFormat(b,"Test.dat");
Console.ReadLine();
}

static void SaveAsBinaryFormat(object obj, string filename)
{
BinaryFormatter binFormat = new BinaryFormatter();

using (Stream fStream = new FileStream(filename,
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, obj);
}
}
}
}


为什么test.dat文件中没有存入任何东西
...全文
136 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeyanbinghappy 2010-06-09
  • 打赏
  • 举报
回复
把ArrayList改成List<T>
houqidian 2010-06-09
  • 打赏
  • 举报
回复
自己顶一个 楼上的不是很懂
houqidian 2010-06-09
  • 打赏
  • 举报
回复
文件时出了问题 还有呢
jackyzsy2008 2010-06-09
  • 打赏
  • 举报
回复
开始是这个文件
FileInfo f = new FileInfo(@"D:\Test.dat")

过一会儿成了这个文件
SaveAsBinaryFormat(b,"Test.dat");

你确定此文件是彼文件么?
houqidian 2010-06-09
  • 打赏
  • 举报
回复
继续顶 已经有眉目了
铛铛 2010-06-09
  • 打赏
  • 举报
回复
        ///   把对象序列化并返回相应的字节   
public static byte[] SerializeObject(object objectToSerialize)
{
if (objectToSerialize == null)
return null;
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, objectToSerialize);
memoryStream.Position = 0;
byte[] read = new byte[memoryStream.Length];
memoryStream.Read(read, 0, read.Length);
memoryStream.Close();
return read;
}
  • 打赏
  • 举报
回复
学习的
jianshao810 2010-06-09
  • 打赏
  • 举报
回复
跟踪一下就出来了。没这么复杂吧。应该是没找到地址
足球中国 2010-06-09
  • 打赏
  • 举报
回复
跟踪一下就出来了。没这么复杂吧。
zzx509 2010-06-09
  • 打赏
  • 举报
回复
FileInfo f = new FileInfo(@"D:\Test.dat");
SaveAsBinaryFormat(b,"Test.dat");
看看你先后写的两个文件,后一个传入的文件名是在你的程序启动路径下,不一定是D盘根目录吧。
ares1986 2010-06-09
  • 打赏
  • 举报
回复

using (Stream fStream = new FileStream(filename,
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fStream, obj);
fStream.Flush();
fStream.Close();
}
}

wuyq11 2010-06-08
  • 打赏
  • 举报
回复
MyObject obj = new MyObject();
  IFormatter formatter = new BinaryFormatter();
  Stream stream = new FileStream("", FileMode.Create,
  FileAccess.Write, FileShare.None);
  formatter.Serialize(stream, obj);
  stream.Close();
List<T> list= new List<T>();
XmlSerializer ser = new XmlSerializer(list.GetType());
ser.Serialize(new FileStream(@"a.xml", FileMode.Create), list);

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧