提个序列化保存的问题!

henryfan1 2005-01-26 09:57:39
现在遇到一个问题,我把一个对象序列化后转成string,
把string值给textbox(win).text,可是textbox什么值的没有。
想问下textbox.text会不会限制某些字符的。
Byte[] bytes = SerializeObject(pObj);
string objtxt= System.Text.Encoding.Default.GetString(bytes);
return objtxt;
我是通过以上代码把byte转成string的.
...全文
211 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
Convert.ToBase64String(bytes);
用这个可以解决问题了,谢谢:)
sprrav 2005-01-26
  • 打赏
  • 举报
回复
是很奇怪,str为底下,textBox1.Text = str;
但是,监视到textBox1.Text==“”
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0System.Collections.Hashtable\a\0\0\0\nLoadFactor\aVersion\bComparerHashCodeProvider\bHashSizeKeysValues\0\0\0\v\bSystem.Collections.IComparer$System.Collections.IHashCodeProvider\b霶8?\0\0\0\n\n\v\0\0\0\t\0\0\0\t\0\0\0\0\0\0\0\0\0\b\b\0\0\0\0\0\0\0\0\0\0\0\0\bI Love U\v"
saucer 2005-01-26
  • 打赏
  • 举报
回复
that sounds really weird, I don't have VS.NET at hand, so I cannot test it for you

and I assume

this.textBox1.SelectionStart = 0;
this.textBox1.SelectedText = str;

doesn't work either


what about

Byte[] bytes = SerializeObject(pObj);

this.textBox1.Text = Convert.ToBase64String(bytes);

??
Bmonkey 2005-01-26
  • 打赏
  • 举报
回复
mark
shuaizhen 2005-01-26
  • 打赏
  • 举报
回复
up
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
Class1 cls = new Class1();
cls.Name ="henry";
string str =HFSoft.Serialization.Serializable.SingleOjb.SerializeObjectToString(cls);
MessageBox.Show(str.Length.ToString()); value:163
this.textBox1.Text = str;
MessageBox.Show(textBox1.Text.Length.ToString());value:0
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
Class1 cls = new Class1();
cls.Name ="henry";
string str =HFSoft.Serialization.Serializable.SingleOjb.SerializeObjectToString(cls);
this.textBox1.Text = str;
MessageBox.Show(textBox1.Text.Length.ToString());
str是有值的,
show的值是0;
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
public byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;


}
saucer 2005-01-26
  • 打赏
  • 举报
回复
maybe those characters are not displayable, what does the following say? (assume this is winform)

MessageBox.Show(this.textBox1.Text.Length);
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
直接序列化成string,然后通过string反回序化是没问题。
在WEB下的TextBox也不存在这情况。就是win下的textbox,RichTextBox都有这个问题。
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
Class1 cls = new Class1();
cl1.Name ="henry";
string str =HFSoft.Serialization.Serializable.SingleOjb.SerializeObjectToString(cl1);
this.textBox1.Text = str;
调试时str是有值的,但给了textbox后,textbox什么都没有显示出来。
从textbox获取text是空值来的。
saucer 2005-01-26
  • 打赏
  • 举报
回复
what is bytes.Length?

how did you serialize the object? if it is MemoryStream, did you set its Position property = 0 after you serialize the object? show your code?
henryfan1 2005-01-26
  • 打赏
  • 举报
回复
在Web的TextBox是可以的。

110,534

社区成员

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

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

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