C# 是否可以序列化 LINQ 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/332390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:33:45  来源:igfitidea点击:

Is it possible to Serialize a LINQ object?

c#linqlinq-to-sqlserialization

提问by Mykroft

I'd like to serialize some LINQ generated objects and store them in a table as a binary field (Never you mind why). I'd like to be able to write some code that looks something like this:

我想序列化一些 LINQ 生成的对象并将它们作为二进制字段存储在表中(不要介意为什么)。我希望能够编写一些看起来像这样的代码:

SerialTestDataContext db = new SerialTestDataContext();

relation_table row = db.relation_tables.First();

MemoryStream memStream = new MemoryStream();
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(memStream, row);
Console.WriteLine("Serilized successfully");

TestTable tt = new testTable();
tt.data = new System.Data.Linq.Binary(memStream.ToArray());
db.testTables.InsertOnSubmit(tt);
db.SubmitChanges();
Console.WriteLine("Inserted successfully");

Currently that fails even though I've marked the generated classes as [Serializable] because one of the LINQ inherited classes is not. Is it even possible to do this?

目前即使我已将生成的类标记为 [Serializable] 也失败了,因为 LINQ 继承的类之一不是。甚至有可能做到这一点吗?

采纳答案by Marc Gravell

With linq-to-sql (from tags), then yes: you can mark the dmbl as serializable, which uses the [DataContract]/[DataMember] approach. You do this by setting the "Serialization Mode" to "Unidirectional" in the designer, or you can do it in the dbml itself:

使用 linq-to-sql(来自标签),是的:您可以将 dmbl 标记为可序列化,它使用 [DataContract]/[DataMember] 方法。您可以通过在设计器中将“序列化模式”设置为“单向”来执行此操作,或者您可以在 dbml 本身中执行此操作:

<Database ... Serialization="Unidirectional">...

You can then use DataContractSerializer or NetDataContractSerializer to write this (as xml or binary respectively). If you need something portable (i.e. not MS/.NET specific), then protobuf-netwill serialize data-contracts using the "protocol buffers" spec.

然后,您可以使用 DataContractSerializer 或 NetDataContractSerializer 来编写它(分别作为 xml 或二进制文件)。如果您需要可移植的东西(即不是 MS/.NET 特定的),那么protobuf-net将使用“协议缓冲区”规范序列化数据合同。

回答by Mykroft

Linq classes are partial classes. You can change the definition to mark the classes as implementing ISerializableand then provide the code...

Linq 类是部分类。您可以更改定义以将类标记为实现ISerializable,然后提供代码...

public partial class User : ISerializable
{
  // implement GetObjectData here
}

There might still be issues with deserialization, however (I'm not 100% on this). An alternative is to use xml serialization and implement IXmlSerializable, which has methods for serializing and deserializing, allowing you to control the entire process...

但是,反序列化可能仍然存在问题(我不是 100% 对此)。另一种方法是使用 xml 序列化并实现IXmlSerializable,它具有序列化和反序列化的方法,允许您控制整个过程......

回答by Mykroft

Following Marc Gravell's advice I wrote the following two blocks of code which worked beautifully:

按照 Marc Gravell 的建议,我编写了以下两段代码,它们运行良好:

relation_table row = db.relation_tables.First();

MemoryStream memStream = new MemoryStream();
NetDataContractSerializer ndcs = new NetDataContractSerializer();
ndcs.Serialize(memStream, row);

byte[] stuff = memStream.toArray();

memStream = new MemoryStream(stuff);
row = ndcs.Deserialize(memStream);

db.relation_tables.Attach(row);
Console.WriteLine(row.data_table1.somedata + ": " + row.more_data);

The Attach() call fills in the foreign key dependencies for me and associates the item with the data context.

Attach() 调用为我填充外键依赖项并将项目与数据上下文相关联。