RemotingException { public CsdnNotFoundException(string str) : base(str) { } } 微软建议继承ISerializable ,标记特性 [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public CsdnNotFoundException ISerializable 那么为什么在使用 Serializable 特性还需要继承 ISerializable ,因为继承 ISerializable 就可以在一个构造函数xx([NotNull] 关于 ISerializable 请看 c# - What is the point of the ISerializable interface?
RemotingException { public CsdnNotFoundException(string str) : base(str) { } } 微软建议继承ISerializable ,标记特性 [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public CsdnNotFoundException ISerializable 那么为什么在使用 Serializable 特性还需要继承 ISerializable ,因为继承 ISerializable 就可以在一个构造函数xx([NotNull] 关于 ISerializable 请看 c# - What is the point of the ISerializable interface?
你的类型通过实现ISerializable接口,也能支持运行时序列化,该接口定义如下:
public interface ISerializable
{
///
RemotingException { public CsdnNotFoundException(string str) : base(str) { } } 微软建议继承ISerializable ,标记特性 [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public [Serializable] public class CsdnNotFoundException : RemotingException, ISerializable { public CsdnNotFoundException ISerializable 那么为什么在使用 Serializable 特性还需要继承 ISerializable ,因为继承 ISerializable 就可以在一个构造函数xx([NotNull] 关于 ISerializable 请看 c# - What is the point of the ISerializable interface?
要实现 ISerializable,需要实现 GetObjectData 方法以及一个特殊的构造函数,在反序列化对象时要用到此构造函数。 以下代码示例说明了如何在前一部分中提到的 MyObject 类上实现 ISerializable。 如果基对象实现了 ISerializable,则派生类应调用其基对象的 GetObjectData 方法。 如果基类实现了 ISerializable,则应调用基类的构造函数,以使基础对象可以还原其变量。 如果从实现了 ISerializable 的类派生出一个新的类,则只要新的类中含有任何需要序列化的变量,就必须同时实现构造函数以及 GetObjectData 方法。
你可以看看关于实现ISerializable接口来使自己的类可以被序列化的做法。 如果基类实现了 ISerializable,则应调用基类的构造函数,以使基础对象可以还原其变量。 7、从实现了 ISerializable 的类派生出一个新的类 果从实现了 ISerializable 的类派生出一个新的类,则只要新的类中含有任何需要序列化的变量,就必须同时实现构造函数以及 GetObjectData 特别是对于未实现 ISerializable 的类更应如此。 在这种情况下,建议实现 ISerializable 并仅序列化所要求的字段。
要实现 ISerializable,需要实现 GetObjectData 方法以及一个特殊的构造函数,在反序列化对象时要用到此构造函数。 以下代码示例说明了如何在前一部分中提到的 MyObject 类上实现 ISerializable。 如果基对象实现了 ISerializable,则派生类应调用其基对象的 GetObjectData 方法。 如果基类实现了 ISerializable,则应调用基类的构造函数,以使基础对象可以还原其变量。 如果从实现了 ISerializable 的类派生出一个新的类,则只要新的类中含有任何需要序列化的变量,就必须同时实现构造函数以及 GetObjectData 方法。
单实例类型代码演示: [Serializable] public class Singleton : ISerializable { private static readonly Singleton 格式化器的Serialize方法,在序列化的时候检测到Singleton类型实现了ISerializable接口,就会调用接口的GetObjectData方法。 5. 序列化到不同类型代码演示: [Serializable] class MyClass1 : ISerializable { public int MyProperty1; void ISerializable.GetObjectData MyClass2不会被序列化,所以不用提供ISerializable.GetObjectData方法的实现。 反序列到不同类型代码演示: [Serializable] class MyClass1 : ISerializable, IObjectReference { public int MyProperty1
确实在.NET CORE 发布之后,Exception 已经不在推荐实现 ISerializable 接口。让我们细说一下。 BinaryFormatter security vulnerabilities 上一篇我们谈论了这么多,其实都是在说 ISerializable 的 patten。 ISerializable 主要的作用就是给 BinaryFormatter 序列化器提供指示如何进行序列化/反序列化。也就是说这个接口基本上就是给 BinaryFormatter 设计的。 那么既然 BinaryFormatter 在目前已经不在推荐使用,自然我们的自定义 Exception 也不用遵循 ISerializable patten 了。
大家都知道,我们具有三种定义可序列化类型的方式:在类型上应用SerializableAttribute特性;应用DataContractAttribute/DataMemberAttribute特性和实现ISerializable 当你通过继承一个现有的类来定义你需要被序列化的类,如果这个父类实现了ISerializable接口,如果定义不当,就会出现反序列化的问题。而且这个我们可能经常都不注意。 context) { } 6: } 从新运行我们的程序,你就会得到想要的输出结果: 1: 001: Foo 2: 002: Bar 3: 003: Baz 如果一个类型实现了ISerializable
ConsoleApplication1.序列化和持久化 { [Serializable] public class MyClass : IDeserializationCallback, ISerializable ISerializable接口 using System; using System.Runtime.InteropServices; namespace System.Runtime.Serialization [ComVisible(true)] public interface ISerializable { // 摘要: // 使用将目标对象序列化所需的数据填充 GetObjectData(SerializationInfo info, StreamingContext context); } } 如果我们想更进一步的控制序列化和反序列化过程,那么我们就来实现ISerializable { this.number = (string)info.GetValue("number", typeof(string)); } 我们实现了ISerializable
定义泛型接口,约束T必须实现当前接口: interface ISerializable<T> where T : ISerializable<T> { static T? Deserialize(string json) => JsonSerializer.Deserialize<T>(json); } class Student : ISerializable<Student { get; set; } public override string ToString() => $"Id: {Id}, Name: {Name}"; } var student = ISerializable
提交 PR 的时候,sonarqube 提示这几个自定义异常不符合 ISerializable patten. 花了点时间稍微研究了一下,把这个问题解了。今天在此记录一下,可能大家都会帮助到大家。 public class NewException : BaseException, ISerializable { public NewException() { // ErrorCode = info.GetString("ErrorCode"); } 这个 GetObjectData 方法是 ISerializable 接口提供的方法,所以基类里肯定有实现 把需要序列化的字段添加到 SerializationInfo 对象上,同样不要忘记调用基类的 GetObjectData 这个问题虽然在自定义 Exception 上暴露出来,其实可以推广到所有实现 ISerializable
为了自定义序列化行为,必须实现ISerializable接口。实现这个接口要实现 <? /// [Serializable] public class Field:ISerializable { private string name="";
Serializable] public class TextTemplatingSession : Dictionary<string, Object>, ITextTemplatingSession, ISerializable context) { Id = (Guid)info.GetValue("Id", typeof(Guid)); } void ISerializable.GetObjectData
GetObjectData 实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary<TKey, TValue> OnDeserialization 实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
context) { ChineseName=string.Format("{0}{1}",LastName,FirstName); } } 3.使用继承ISerializable 接口更灵活地控制序列化过程# 除了利用特性Serializable之外,我们还可以注意到在序列化的应用中,常常会出现一个接口ISerializable。 、OnSerializingAttribute、NonSerialized等特性不能完全满足自定义序列化的要求,那就需要继承ISerializable了。 例如我们要将一个对象反序列化成为另外一个对象,就要都实现ISerializable接口,原理其实很简单,那就是在一个对象的GetObjectData方法中处理序列化,在另一个对象的受保护构造方法中反序列化 4.实现ISerializable的子类型应负责父类的序列化# 我们将要实现的继承自ISerializable的类型Employee有一个父类Person,假设Person没有实现序列化,而现在子类Employee
用于控制在序列化和反序列化期间使用的实际类型 StreamingContext Context 序列化流上下文 其中states字段包含了序列化的来源和目的地 BinaryFormatter序列化的生命周期和事件 ISerializable 我们先来看看实现ISerializable 接口的类 序列化、反序列化调用流程 using System; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary ; using System.Security.Permissions; namespace ConsoleAppi1; [Serializable] public class Person:ISerializable fstream.Position = 0; binFormatterD.Deserialize(fstream); } } 运行结果如下 可以发现如果实现了ISerializable System.Runtime.Serialization.Formatters.Binary; using System.Security.Permissions; namespace ConsoleAppi1; [Serializable] public class Person:ISerializable
高级技巧自定义序列化通过实现 ISerializable 接口或使用特性,可以自定义对象的序列化和反序列化过程。 [Serializable]public class Person : ISerializable{ public string Name { get; set; } public int
可以通过实现 ISerializable 接口来解决这个问题。 [Serializable]public class Singleton : ISerializable{ private static readonly Singleton _instance