简体   繁体   English

条件C#二进制序列化

[英]Conditional C# Binary Serialization

I am using BinaryFormatter to serialize a class and its variables by condition. 我正在使用BinaryFormatter按条件序列化一个类及其变量。 For example: 例如:

[Serializable]
public class Class1
{
private Class2 B;
...
}

[Serializable]
public class Class2{...}

I want the variable B to be serialized only when remoting time, but not when i serialize it to file storage. 我希望仅在远程处理时间将变量B序列化,而不是在将其序列化到文件存储时才进行序列化。 Questions: 问题:
1) I know that in XmlSerialization we can use [XmlIgnore] and {PropertyName}Specified to ignore the property conditionally. 1)我知道在XmlSerialization中,我们可以使用[XmlIgnore]和{PropertyName} Specified来有条件地忽略该属性。 Is that a equivalent method for [NonSerialized]? 这是[NonSerialized]的等效方法吗?
2) For a class with [Serializable] attribute, how to ignore it at the runtime? 2)对于具有[Serializable]属性的类,如何在运行时忽略它?

I wrote a rather simple but extensible framework to solve this sort of problem using bindings. 我编写了一个相当简单但可扩展的框架,以使用绑定来解决此类问题。 Not sure I completely understand but this is possible: 不确定我是否完全理解,但这是可能的:

public class Class1
{
  [Ignore]
  public bool IsRemoting { get; set; }

  [SerializeWhen("IsRemoting", true)]
  public Class2 B;
}

http://binaryserializer.codeplex.com http://binaryserializer.codeplex.com

  1. There is no such method. 没有这种方法。 You can control serialization by implementing ISerializable , and if you do you will know which serialization context is active (remoting, file etc.) 您可以通过实现ISerializable来控制序列化,如果您这样做,您将知道哪个序列化上下文处于活动状态(远程处理,文件等)。
  2. AFAIK no way to do it, why do you want this? AFAIK没办法,为什么要这样?

Generally speaking I advise you against using BinaryFormatter . 一般来说,我建议您不要使用BinaryFormatter It is a maintenance headache if there ever was one. 如果有的话,这是一个维护难题。 Use XML serialization or some kind of protocol buffers. 使用XML序列化或某种协议缓冲区。

  1. As already mentioned, it doesn't exist. 如前所述,它不存在。 You could code your way out of although it is a bit messy (that is if you don't want to implement the ISerializable interface as already suggested). 尽管有些混乱(即使您不想像已经建议的那样实现ISerializable接口),也可以编写出自己的方法。

     [Serializable] public class ClassA { [OnSerializing] private void OnSerializing(StreamingContext context) { //Set BSerialized = B based on context or some internal boolean BSerialized = B; } [OnSerialized] private void OnSerialized(StreamingContext context) { //Clear BSerialized BSerialized = null; } [OnDeserialized] private void OnDeserialized(StreamingContext context) { //Restore B from BSerialized B = BSerialized; BSerialized = null; } [NonSerialized] private ClassB B; private ClassB BSerialized; } [Serializable] public class ClassB { } 
  2. You can't ignore it. 您不能忽略它。 You can only change properties on attributes at runtime and since the NonSerialized attribute doesn't take a true / false argument, you cannot do anything about it runtime. 您只能在运行时更改属性的属性,并且由于NonSerialized属性不采用true / false参数,因此您无法在运行时对其进行任何操作。

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

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