2

我正在寻找一种非侵入式的方式来强制反序列化在以下情况下失败:

  • 该类型未在强命名程序集中定义。
  • 使用 BinaryFormatter。
  • 自序列化以来,类型已被修改(例如,已添加属性)。

下面是一个失败的 NUnit 测试形式的问题的说明/重现。我正在寻找一种通用的方法来使这个通过而不修改Data类,最好只设置BinaryFormatter在序列化和/或反序列化期间。我也不想涉及序列化代理,因为这可能需要每个受影响类型的特定知识。

在 MSDN 文档中找不到任何对我有帮助的东西。

[Serializable]
public class Data
{
  public string S { get; set; }
}

public class DataSerializationTests
{
    /// <summary>
    /// This string contains a Base64 encoded serialized instance of the
    /// original version of the Data class with no members:
    /// [Serializable]
    /// public class Data
    /// { }
    /// </summary>
    private const string Base64EncodedEmptyDataVersion =
        "AAEAAAD/////AQAAAAAAAAAMAgAAAEtTc2MuU3Rvcm0uRGF0YS5UZXN0cywgV"+
        "mVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2"+
        "VuPW51bGwFAQAAABlTc2MuU3Rvcm0uRGF0YS5UZXN0cy5EYXRhAAAAAAIAAAAL";

    [Test]
    public void Deserialize_FromOriginalEmptyVersionFails()
    {
        var binaryFormatter = new BinaryFormatter();
        var memoryStream = new MemoryStream(Convert.FromBase64String(Base64EncodedEmptyDataVersion));

        memoryStream.Seek(0L, SeekOrigin.Begin);

        Assert.That(
            () => binaryFormatter.Deserialize(memoryStream),
            Throws.Exception
        );
    }
}
4

1 回答 1

0

我在这里推荐一种“Java”方式 - 在每个可序列化的类中声明 int 字段,例如private int _Serializable = 0;并检查您的当前版本和序列化版本是否匹配;更改属性时手动增加。如果您坚持使用自动化方式,则必须存储大量元数据并检查当前元数据和持久化元数据是否匹配(对序列化数据的性能/大小造成额外负担)。

这是自动描述符。基本上,您必须将TypeDescriptor实例存储为二进制数据的一部分,并在检索时检查持久化是否对针对当前TypeDescriptor的序列化()有效。IsValidForSerializationTypeDescriptor

var persistedDescriptor = ...;
var currentDescriptor = Describe(typeof(Foo));
bool isValid = persistedDescriptor.IsValidForSerialization(currentDescriptor);

[Serializable]
[DataContract]
public class TypeDescriptor
{
  [DataMember]
  public string TypeName { get; set; }
  [DataMember]
  public IList<FieldDescriptor> Fields { get; set; }

  public TypeDescriptor()
  {
    Fields = new List<FieldDescriptor>();
  }

  public bool IsValidForSerialization(TypeDescriptor currentType)
  {
    if (!string.Equals(TypeName, currentType.TypeName, StringComparison.Ordinal))
    {
      return false;
    }
    foreach(var field in Fields)
    {
      var mirrorField = currentType.Fields.FirstOrDefault(f => string.Equals(f.FieldName, field.FieldName, StringComparison.Ordinal));
      if (mirrorField == null)
      {
        return false;
      }
      if (!field.Type.IsValidForSerialization(mirrorField.Type))
      {
        return false;
      }
    }
    return true;
  }
}

[Serializable]
[DataContract]
public class FieldDescriptor
{
  [DataMember]
  public TypeDescriptor Type { get; set; }
  [DataMember]
  public string FieldName { get; set; }
}

private static TypeDescriptor Describe(Type type, IDictionary<Type, TypeDescriptor> knownTypes)
{
  if (knownTypes.ContainsKey(type))
  {
    return knownTypes[type];
  }

  var descriptor = new TypeDescriptor { TypeName = type.FullName, Fields = new List<FieldDescriptor>() };
  knownTypes.Add(type, descriptor);
  if (!type.IsPrimitive && type != typeof(string))
  {
    foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).OrderBy(f => f.Name))
    {
      var attributes = field.GetCustomAttributes(typeof(NonSerializedAttribute), false);
      if (attributes != null && attributes.Length > 0)
      {
        continue;
      }

      descriptor.Fields.Add(new FieldDescriptor { FieldName = field.Name, Type = Describe(field.FieldType, knownTypes) });

    }
  }
  return descriptor;
}

public static TypeDescriptor Describe(Type type)
{
  return Describe(type, new Dictionary<Type, TypeDescriptor>());
}    

我还考虑了一些缩短持久元数据大小的机制——比如从 xml-serialized 或 json-serialized TypeDescriptor 计算 MD5;但在这种情况下,新属性/字段会将您的对象标记为不兼容序列化。

于 2015-04-21T12:31:29.340 回答