protobuf-net build v2.0.0.580 无法序列化 System.Xml.Linq.XElement



我刚刚用最新版本(版本 2.0.0.580)替换了我在代码库(版本 2.0.0.480)中使用的以前的 protobuf-net 构建 - 由于在最新的 protobuf 版本中没有针对 .Net 4.0 或 .Net 4.5 的特定构建,我在我的项目中引用了 net30 full dll。

当尝试序列化包含System.Xml.Linq.XElement的复杂类型时,我从protobuf序列化程序获得InvalidOperationException-异常消息指出:"没有为类型定义序列化程序:System.Xml.Linq.XElement"

我假设缺少 XElement 的序列化程序与没有 .Net 4.0 构建的事实有关(因为我在我的项目中引用了 System.Xml.Linq.dll 的 v4.0.30319)。

我的项目面向 .NET 4.5 平台 - 它在引用 protobuf-net V2.0.0.480 net40 构建时工作正常。

看起来与"可解析类型"有关 - 没有内置处理但满足某些模式的东西,允许将它们视为string。这在过去引起了一些问题,因此可以选择通过.AllowParseableTypes支持:

static void Main()
{
    XElement el = XElement.Parse("<xml />");
    var model = TypeModel.Create(); // store and re-use this; don't use
    model.AllowParseableTypes = true; // a new one each time! (very bad)
    var foo = new Foo { Bar = el };
    var clone = (Foo) model.DeepClone(foo);
    var cloneEl = clone.Bar;
}
[ProtoContract]
class Foo
{
    [ProtoMember(1)]
    public XElement Bar { get; set; }
}

请注意,我必须在此处声明Foo的原因是,看起来"根对象"处理(与处理成员/子对象完全不同)没有检查可解析的类型;我将不得不调查为什么不并修复它。我希望以下内容会起作用(目前没有,但可能应该):

static void Main()
{
    XElement el = XElement.Parse("<xml />");
    var model = TypeModel.Create(); // store and re-use this; don't use
    model.AllowParseableTypes = true; // a new one each time! (very bad)
    var cloneEl = (XElement)model.DeepClone(el);
}

相关内容

最新更新