1

我刚刚用最新版本(版本 2.0.0.580)替换了我在代码库(版本 2.0.0.480)中使用的以前的 protobuf-net 版本 - 因为没有针对 .Net 4.0 或 .Net 4.5 的特定版本最新的 protobuf 版本,我在我的项目中引用了 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 构建时它工作正常。

4

1 回答 1

0

看起来与“可解析类型”有关 - 没有内置处理的东西,但符合某些模式,允许它们被视为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这里声明 a 的原因是它看起来像“根对象”处理(这与处理成员/子对象完全不同)没有检查可解析类型;我将不得不调查为什么不并修复它。我希望以下内容会起作用(目前没有,但可能应该):

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);
}
于 2012-09-28T10:32:03.730 回答