蝈蝈俊.net

-- 用随笔来记录自己的技术感触
随笔 - 673, 评论 - 4416, 引用 - 276

导航

关于

记录自己的技术心得

标签

每月存档

最新留言

  • Cjlzcnuv
    perfect design thanks <a href=" http://www.wikio.com/user/aloimejug/bio "&a...
    by Cjlzcnuv(匿名) on 2010/3/22 0:43:37
  • Omhobhjt
    It's serious <a href=" http://www.wikio.com/user/oehulopyji/bio ">fr...
    by Omhobhjt(匿名) on 2010/3/22 0:43:36
  • Eiybkdpo
    This site is crazy :) <a href=" http://www.wikio.com/user/ofypaile/bio "&am...
    by Eiybkdpo(匿名) on 2010/3/22 0:43:34
  • Hwpphius
    I'm happy very good site <a href=" http://www.wikio.com/user/geebypubeel/bio &qu...
    by Hwpphius(匿名) on 2010/3/21 23:56:57
  • Aefdrkin
    Best Site Good Work <a href=" http://www.wikio.com/user/inohedos/bio "&...
    by Aefdrkin(匿名) on 2010/3/21 23:56:56
  • Kcjmgqgv
    magic story very thanks <a href=" http://www.wikio.com/user/riobakahol/bio &quot...
    by Kcjmgqgv(匿名) on 2010/3/21 23:56:55
  • Iefdndcu
    real beauty page <a href=" http://www.wikio.com/user/geebypubeel/bio "&...
    by Iefdndcu(匿名) on 2010/3/21 22:39:25
  • Uuorbsjt
    This site is crazy :) <a href=" http://www.wikio.com/user/inohedos/bio "&am...
    by Uuorbsjt(匿名) on 2010/3/21 22:39:08
  • Kkckitku
    real beauty page <a href=" http://www.wikio.com/user/geebypubeel/bio "&...
    by Kkckitku(匿名) on 2010/3/21 22:38:54
  • Dqcrmtcw
    Best Site Good Work <a href=" http://www.wikio.com/user/okefykyhyac/bio "&a...
    by Dqcrmtcw(匿名) on 2010/3/21 21:50:46

广告

 

先看一个现象:
如果你有这样的一个WebService 方法:

[WebMethod]
public void myTest(System.Collections.Specialized.NameValueCollection col)
{
.....
}

那你在请求这个WebService 方法的时候,会收到如下异常:

To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Collections.Specialized.NameValueCollection does not implement Add(System.String).

详细的错误发生点如下:
[InvalidOperationException: To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy.
System.Collections.Specialized.NameValueCollection does not implement Add(System.String).]
System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String memberInfo) +849
System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference) +857
System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) +135
System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflec tionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel) +78
System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlRefle ctionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel) +280


这是因为对 ICollection 接口的类进行序列化的一些特殊要求:

XmlSerializer 可以以不同方式处理实现 IEnumerable 或 ICollection 的类,条件是 这些类满足某些要求,如下所示。

1、实现 IEnumerable 的类必须实现带单个参数的公共 Add 方法。Add 方法的参数必须与 从 GetEnumerator 方法返回的 IEnumerator.Current 属性所返回的类型一致(多态) 。

2、除实现 IEnumerable 外还实现 ICollection 的类(如 CollectionBase)必须有一个 值为整数的公共 Item 索引属性(在 C# 中为索引器),并且必须有一个整数类型的公 共 Count 属性。传递给 Add 方法的参数必须与从 Item 属性返回的类型相同或与该类 型的某个基的类型相同。

3、对于实现 ICollection 的类,要序列化的值将从索引的 Item 属性检索,而不是通过 调用 GetEnumerator 来检索。另外,除返回另一个集合类(实现 ICollection 的集合 类)的公共字段之外,将不序列化其他公共字段和属性。

ICollection 接口的类要可以被序列化, 该类必须包含 Add 方法和要序列化的 Item 属性(C# 索引器)。


上面的WebService 例子出现异常是因为:

NameValueCollection 并不直接实现 ICollection 接口。相反,NameValueCollection 扩展 NameObjectCollectionBase。这样,就会实现 ICollection 接口,并且在 NameValueCollection 类中不实现重载 Add(system.string) 方法。

在使用 XMLSerializer 时,XmlSerializer 尝试将 NameValueCollection 序列化或反序列化 为一般 ICollection。因此,它查找默认的 Add(System.String)。如果没有 Add(system.String) 方法,就会发生异常。

参考资料:

PRB:使用 XmlSerializer 序列化 NameValueCollection 对象时出 现“System.InvalidOperationException”错误
http://support.microsoft.com/default.aspx/kb/814187/zh-cn?spid=548&sid=304

序列化实现 ICollection 接口的类
http://msdn2.microsoft.com/zh-cn/library/58a18dwa.aspx

http://www.codecomments.com/archive321-2006-2-823224.html

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_ 21585935.html

打印 | 张贴于 2006-10-31 18:22:00 | Tag:.net 编程心得  技术随笔  网站开发管理相关内容  VS2005相关

留言反馈

#回复: ICollection 接口的类序列化的问题。 编辑
参考资料中最后一个链接应该是下面地址
上面多了一个空格 %20

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_ 21585935.html
2008-02-01 10:25:00 | [匿名:ghj1976]
#re: ICollection 接口的类序列化的问题。 编辑
不错
2006-11-01 16:27:00 | [匿名:疏水阀]
#re: ICollection 接口的类序列化的问题。 编辑
那么如果我希望可以序列化应该如何做呢?
2006-11-01 15:28:00 | [匿名:冰河の泥鱼]
#re: PHP和IIS 7.0的FastCGI模板 编辑
很详细,谢谢啊
2006-11-01 15:27:00 | [匿名:天天影院]
#re: 编辑
有价值。
2006-11-01 12:01:00 | [匿名:e商]
#re: ICollection 接口的类序列化的问题。 编辑
所有集合中,ArrayList 是可以直接序列化的。

NameValueCollection 、Hashtable 是不可以序列化的。

如果 WebService 方法中用 Hashtable 做参数,会报错:

The type System.Collections.Hashtable is not supported because it implements IDictionary.
2006-10-31 18:27:00 | [匿名:ghj1976]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0