有条件的C#二进制序列化 [英] Conditional C# Binary Serialization

查看:153
本文介绍了有条件的C#二进制序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的BinaryFormatter序列化类和条件的变量。例如:

I am using BinaryFormatter to serialize a class and its variables by condition. For example:

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

[Serializable]
public class Class2{...}

我想只序列化的变量b远程处理的时候,而不是当我序列化到文件存储。
的问题:结果
1)我知道,在XmlSerialization我们可以使用[XmlIgnore]和{}属性名指定有条件忽略的财产。是,[非序列化]一个等效的方法?结果
2)对于[Serializable]属性,如何在运行时忽略它?

I want the variable B to be serialized only when remoting time, but not when i serialize it to file storage. Questions:
1) I know that in XmlSerialization we can use [XmlIgnore] and {PropertyName}Specified to ignore the property conditionally. Is that a equivalent method for [NonSerialized]?
2) For a class with [Serializable] attribute, how to ignore it at the runtime?

推荐答案

我写了一个相当简单的,但可扩展的框架来解决使用绑定这类问题。不知道我完全理解,但这是可能的:

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

这篇关于有条件的C#二进制序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆