C# 中的类型“System.Windows.Forms.DataGridViewr”未标记为可序列化 问题
namespaceClientTest{[Serializable]publicclassb_datagridview:DataGridView{publicb_data...
namespace ClientTest
{
[Serializable]
public class b_datagridview : DataGridView
{
public b_datagridview()
{
}
public byte[] ObjectToByte()
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
byte[] b;
formatter.Serialize(ms, this);
ms.Position = 0;
b = new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
}
}以上为我的代码. 我已经添加了[Serializable],为什么还不可以?! 展开
{
[Serializable]
public class b_datagridview : DataGridView
{
public b_datagridview()
{
}
public byte[] ObjectToByte()
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
byte[] b;
formatter.Serialize(ms, this);
ms.Position = 0;
b = new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
}
}以上为我的代码. 我已经添加了[Serializable],为什么还不可以?! 展开
1个回答
展开全部
你要序列化什么?b_datagridview吗?还是datagridview的datasource?
这里有问题: formatter.Serialize(ms, this);
我之前写过的一个给初学者学习的序列化和反序列话的demo 希望对你有帮助
[Serializable]
public class MyObject
{
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
public class test
{
public static void Main()
{
Serialzable();
DisSerialzable();
}
private static void Serialzable()
{
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
private static void DisSerialzable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);
stream.Close();
// Here's the proof.
Console.WriteLine("n1: {0}", obj.n1);
Console.WriteLine("n2: {0}", obj.n2);
Console.WriteLine("str: {0}", obj.str);
}
}
这里有问题: formatter.Serialize(ms, this);
我之前写过的一个给初学者学习的序列化和反序列话的demo 希望对你有帮助
[Serializable]
public class MyObject
{
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
public class test
{
public static void Main()
{
Serialzable();
DisSerialzable();
}
private static void Serialzable()
{
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
private static void DisSerialzable()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);
stream.Close();
// Here's the proof.
Console.WriteLine("n1: {0}", obj.n1);
Console.WriteLine("n2: {0}", obj.n2);
Console.WriteLine("str: {0}", obj.str);
}
}
追问
我发现只要是windowform下的控件,都无法序列化,,,最后我自己写了一个column的属性类,再讲这个类序列化,这个到时解决了这个问题.不知道我的控件无法直接序列化结论是否正确.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询