[英]Binary serialization without serializable attribute
I want to serailize my object and used BinaryFormatter
class.我想对我的对象进行序列化并使用BinaryFormatter
类。
public static byte[] BinarySerialize(IMessage message)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, message);
return stream.ToArray();
}
}
But when I run the code, throws an exception.但是当我运行代码时,抛出异常。
SerializationException: Object is not marked as serializable. SerializationException: 对象未标记为可序列化。
I think this exception thrown by BinaryFormatter.我认为这个异常是由 BinaryFormatter 抛出的。
I do not want to mark as [Serializable]
my objects.我不想将我的对象标记为[Serializable]
。 Or my library users may forget mark as [Serializable]
their own Messages.或者我的图书馆用户可能忘记将他们自己的消息标记为[Serializable]
。
Is there any other way to binary serialize my objects without using [Serializable] attribute?有没有其他方法可以在不使用 [Serializable] 属性的情况下对我的对象进行二进制序列化?
Since [Serializable] attribute cannot be added runtime, there are nooptions if you want to stick to the .Net built in Serialization.由于无法在运行时添加 [Serializable] 属性,因此如果您想坚持使用 .Net 内置的序列化,则没有选项。
You can你可以
Use an external library such as: http://sharpserializer.codeplex.com/ And by the way, they have moved to GitHub.使用外部库,例如: http : //sharpserializer.codeplex.com/顺便说一下,他们已转移到 GitHub。 See: https://github.com/polenter/SharpSerializer请参阅: https : //github.com/polenter/SharpSerializer
public static byte[] BinarySerialize(IMessage message) { using (var stream = new MemoryStream()) { var serializer = new SharpSerializer(true); serializer.Serialize(message, stream ); return stream.ToArray(); } }
Use JSON serialization使用 JSON 序列化
In addition to the other answers regarding 3rd party libs, depending on your needs you may choose to use XmlSerializer
.除了有关 3rd 方库的其他答案外,根据您的需要,您可以选择使用XmlSerializer
。 (Better yet use a JSON serializer that doesn't require the SerializeableAttribute
.) (最好使用不需要SerializeableAttribute
的JSON 序列SerializeableAttribute
。)
These serializers do not require [Serializeable]
.这些序列化器不需要[Serializeable]
。 However, the XmlSerializer
doesn't allow serialization of interfaces either.但是, XmlSerializer
也不允许接口序列化。 If you are good with concrete types it works.如果你擅长具体类型,它就可以工作。 Compare serialization options . 比较序列化选项。
EG EG
void Main()
{
var serialized = Test.BinarySerialize(new SomeImpl(11,"Hello Wurld"));
}
public class Test
{
public static string BinarySerialize(SomeImpl message)
{
using (var stream = new StringWriter())
{
var formatter = new XmlSerializer(typeof(SomeImpl));
formatter.Serialize(stream, message);
return stream.ToString().Dump();
}
}
}
public class SomeImpl
{
public int MyProperty { get;set;}
public string MyString { get;set; }
public SomeImpl() {}
public SomeImpl(int myProperty, String myString)
{
MyProperty = myProperty;
MyString = myString;
}
}
To avoid Net4x built in Serialization that require the [Serializable] attribute, use Newtonsoft.Json or System.Text.Json in netcore 3.1+ or Net5为避免需要 [Serializable] 属性的 Net4x 内置序列化,请在 netcore 3.1+ 或 Net5 中使用 Newtonsoft.Json 或 System.Text.Json
string json= JsonConvert.SerializeObject(message);
//or System.Text.Json in netcore 3.1+
string json= System.Text.Json. JsonSerializer.Serialize(message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.