简体   繁体   English

我们可以在linux机器上读取.NET中序列化的对象吗?

[英]Can we read an object serialized in .NET in a linux machine?

I need to transfer some files written in serialized form as files in a windows machine (C#.NET serialization) to a linux machine. 我需要将一些以序列化形式编写的文件作为Windows机器(C#.NET序列化)中的文件传输到linux机器。 How can I achieve this? 我怎样才能做到这一点? I need to use perl/Java/bash in linux side preferably. 我需要在linux端使用perl / Java / bash。

Edit: To be clearer, files are text files.. but binary serialized in .NET. 编辑:为了更清楚,文件是文本文件..但在.NET中二进制序列化。 In linux side, I need to use Perl/Java/Bash to de-serialize and read these files. 在linux方面,我需要使用Perl / Java / Bash来反序列化和读取这些文件。 I have the constraint that the .NET side code cannot be touched.. Anything I do has to be on the linux side.. 我有一个约束,即.NET端代码无法触及..我所做的一切都必须在linux端。

Thanks, 谢谢,

You can deserialise .NET-serialised data on Linux if you have a .NET CLI implementation, such as Mono or DotGNU . 如果您具有.NET CLI实现(例如MonoDotGNU) ,则可以在Linux上反序列化.NET序列化数据。 This way you would be able to write a C# wrapper to handle the deserialisation then, as Brian stated above, reserialise using XML if you want to use the data in a non .NET application. 通过这种方式,您可以编写一个C#包装器来处理反序列化,然后如上所述,如果要在非.NET应用程序中使用数据,则使用XML进行重新序列化。

For .NET, the necessary namespaces and classes are: 对于.NET,必要的命名空间和类是:

BinaryFormatter and FileStream classes: BinaryFormatter和FileStream类:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.IO.FileStream

To deserialise, create an instance of the BinaryFormatter and FileStream classes, loading the serialised data into the FileStream. 要反序列化,请创建BinaryFormatter和FileStream类的实例,将序列化数据加载到FileStream中。 Then call Deserialize on the BinaryFormatter and cast into the necessary data type (I've called it TheClass below): 然后在BinaryFormatter上调用Deserialize并转换为必要的数据类型(我在下面称它为TheClass):

BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.OpenRead(@"InsertFileName");
TheClass classInstance = (TheClass)formatter.Deserialize(file);
file.Close();

XML serialisation using raw XML or SOAP is more interoperable with non .NET apps. 使用原始XML或SOAP的XML序列化可以与非.NET应用程序更加互操作。 SOAP serialisation is available using the SoapFormatter class: 使用SoapFormatter类可以使用SOAP序列化:

System.Runtime.Serialization.Formatters.Soap.SoapFormatter

Serialisation is performed by creating FileStream and SoapFormatter instances and calling the Soapformatter Serialize method. 通过创建FileStream和SoapFormatter实例并调用Soapformatter Serialize方法来执行序列化。 To serialise the classInstance example above: 要序列化上面的classInstance示例:

FileStream file = File.Create(@"InsertFileName");
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(file, classInstance);
file.Close();

Raw XML serialisation is highly customisable but works slightly differently. 原始XML序列化是高度可定制的,但工作方式略有不同。 The XMLSerializer class is used for this purpose: XMLSerializer类用于此目的:

System.Xml.Serialization.XmlSerializer

To serialize TheClass using XML serialisation, you will need instances of XmlSerializer and StreamWriter (in System.IO): 要使用XML序列化序列化TheClass,您将需要XmlSerializer和StreamWriter的实例(在System.IO中):

XmlSerializer serializer = new XmlSerializer(typeof(TheClass));
StreamWriter xmlFile = new StreamWriter(@"InsertFileName");
serializer.Serialize(xmlFile, classInstance);
xmlFile.Close();

Once in XML, either raw or SOAP, other languages such as Java should have little difficulty reading them. 一旦使用XML,无论是原始的还是SOAP,其他语言(如Java)都应该很难阅读它们。 For more info on XML serialisation, see this page on MSDN. 有关XML序列化的更多信息,请参阅MSDN上的此页面

To work with .NET on Linux, the Mono Project have created an IDE called MonoDevelop which works in a similar way to Visual Studio on Windows. 为了在Linux上使用.NET,Mono Project创建了一个名为MonoDevelop的IDE,其工作方式与Windows上的Visual Studio类似。

I hope this infornmation is useful! 我希望这个信息是有用的!

Even though you can't touch the .Net code, you could probably still solve this problem by writing a simple .Net program which takes as input a serialized object and gives as output a reserialized object (using a serialization that is easier to read, such as XML). 即使您无法触摸.Net代码,您仍然可以通过编写一个简单的.Net程序来解决这个问题。该程序将序列化对象作为输入,并将一个重新序列化的对象作为输出(使用更易于阅读的序列化,比如XML)。 If that is not practical, you will be experiencing pain. 如果这不切实际,你将会感受到痛苦。 I'm not aware of a Linux wrapper that can read windows .Net Binary Serialized files, though it is possible that Mono knows how to do it. 我不知道可以读取Windows .Net二进制序列化文件的Linux包装器,尽管Mono可能知道如何操作它。

If you want to do it yourself, you may find Lluis Sanchez Gual's page to be a helpful start at documenting how it works, though it's an old page. 如果你想自己做,你可能会发现Lluis Sanchez Gual的页面是一个有用的开始,记录它是如何工作的,尽管它是一个旧页面。

Serialization contains enough information to pull up the corresponding objects and populate them with date. 序列化包含足够的信息来提取相应的对象并用日期填充它们。

For simple objects you can emulate the deserialization process but there is nothing to help you do so. 对于简单对象,您可以模拟反序列化过程,但没有任何东西可以帮助您这样做。 You need to implement everything yourself. 你需要自己实现一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM