键入XXXX预计不会使用xmlinclude或soapinclude [英] type xxxx not expected use xmlinclude or soapinclude

查看:361
本文介绍了键入XXXX预计不会使用xmlinclude或soapinclude的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种序列化问题的一个奇怪的情况 - 这已经被问了很多次在这个网站,我已经通过了一些对这些问题消失了,并试图平常的物品都无济于事:

I have a curious case of this serialization issue - which has been asked many times on this site and I have gone through a few of these questions and tried the usual items to no avail:


  • 添加[XmlInclude]对类引发错误

  • 删除命名空间

  • 添加不同命名空间为每个类

要进一步的解释,我已经提供了下面我的代码的简化版本。基本上我使用的是 WebServiceHost 对象运行一个RESTful服务,我的一个端点返回作为XML序列化对象(我已经标注了对象[ DataContract] [数据成员] 属性)。这个对象包含一个 SerializableDictionary<字符串对象> 这里其中,值已分类为对象)。我相信这就是为什么它是失败的:

To explain further, I have provided a simplified version of my code below. Essentially I am using a WebServiceHost object to run a RESTful service and one of my endpoints returns an object serialized as XML (I have annotated the object with [DataContract] and [DataMember] attributes). This object contains a SerializableDictionary<string, object> (here) where the value has been typed as object. I believe this is why it is failing:


  • 当值分配原始

  • 做工精细
  • 当我分配一个自定义对象的KV对V,我得到的意外的类型的例外可能是因为串行不知道如何serilaize对象/某种命名空间问题

  • Works fine when the value is assigned a primitive
  • When I assign a custom object to the KV pair V, I get the unexpected type exception probably because the Serializer does not know how to serilaize the object / some sort of namespacing issue

显然,我无法标注Object.cs与 [XmlInclude] 键,因为它是一个服务而且我不是我自己,我不能使用像

Obviously, I am unable to annotate Object.cs with [XmlInclude] and because it is a service and I am not myself serializing I cannot using something like

new Serializer(typeof(...), new Type[] { ... }}

什么我可以做任何想法的?我想过不打字的字典值作为对象,rtaher comething更具体的,但问题是,这个值可以采用原语或cusotm类型的一些代码来解释一下上面:

Any idea's of what I can do? I thought about not typing the dict value as object and rtaher comething more concrete but the problem is that this value can take primitives or cusotm types. Some code to explain the above:

修改更新了下面的代码,以使其更清晰。

Updated the code below to make it more clear

[DataContract]
public class ResponseObject
{
    [DataMember(Name = "data")]
    public SerializableDictionary<string, object> Data { get;set; }

    public ResponseObject()
    {
        Data = new SerializableDictionary<string, object>();
    }
}

...

var d1 = new ResponseObject();
d1.Data.Add("some key", "some value"); //WORKS AND SERIALIZES PERFECLTY

var d2 = new ResponseObject();
d2.Data.Add("some other key", new SomeOtherObjecT());
var d3 = new ResponseObject();
d3.Data.Add("another key", d2);  //THIS THROWS THE UNEXPECTED TYPE ERROR WHEN SEIRLAIZING SomeOtherObject



编辑:的错误是在SerializableDictionary在那里试图序列类型ResponseObject的对象抛出。两者在单独的项目 - 如果这是显著

The error is thrown in SerializableDictionary where it is attempting to serialize an object of type ResponseObject. The two are in seperate projects - if that is significant?

推荐答案

通常情况下,你应该增加一个[XmlInclude]将ResponseObject类。在这种情况下,它不会因为你正在使用的SerializableDictionary的工作。这个类在其实现创建另一个XmlSerializer的,所以并不会在意你的[XmlInclude]的。基本上它只是无法处理您的使用情况。 http://pastebin.com/:你应该从XmlSerializer的到DataContractSerializer的它处理Dictionary类,支持[KnownType]属性,来注册其他类型切换vGLSaxHF 。另外请注意,这是毫无意义添加[DataContract],并在当前的情况下,[数据成员]属性,因为XmlSerializer的会忽略这些属性,他们只DataContractSerializer的使用。或者,如果你不知道如何改变你的序列化(我知道我不是),那么你要么不使用词典或改变SerializableDictionary实现来处理,你要使用(找到每一个行,其中动态对象类型它创建一个新的XmlSerializer)。或者,作为替代,定义一个基类,你将永远放进字典中的所有的对象,并像这样做:

Normally, you should add an [XmlInclude] to the ResponseObject class. In this case, it doesn't work because of the SerializableDictionary that you're using. That class creates another XmlSerializer in its implementation, and therefore it doesn't care about your [XmlInclude]'s. Basically it just cannot handle your use case. You should switch from the XmlSerializer to the DataContractSerializer which handles the Dictionary class and supports the [KnownType] attribute to register additional types: http://pastebin.com/vGLSaxHF . Also note that it's pointless to add [DataContract] and [DataMember] attributes in your current case because the XmlSerializer ignores those attributes, they are used by the DataContractSerializer only. Or if you're not sure how to change your serializer (I know I'm not) then you should either not be using a Dictionary or change the SerializableDictionary implementation to handle the dynamic object types that you want to use (find every line where it creates a new XmlSerializer). Or, as an alternative, define a base class for all your objects that you will ever put into the dictionary and do it like this:

[XmlInclude(typeof(Class1), XmlInclude(typeof(Class2)), etc]
public class AbstractBase { }

public class Class1 : AbstractBase { ... }

public class Class2 : AbstractBase { ... }

public class BigClass {
    public SerializableDictionary<string, AbstractBase> Dictionary { get; set; }
}

此方式,当SerializableDictionary创建自己的XmlSerializer,它将识别AbstractBase并从那里,所有它的后代。

This way, when the SerializableDictionary creates its own XmlSerializer, it will recognize the AbstractBase and from there, all of its descendants.

这篇关于键入XXXX预计不会使用xmlinclude或soapinclude的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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