蝈蝈俊.net

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

导航

关于

这里是我的技术Blog,下一代CSDN社区Blog在 http://blog.csdn.net/ghj1976/

标签

每月存档

最新留言

  • re:学习笔记:7种结构型设计模式简单对比
    <p>最新在家创业系统 ----刚从国外引进,市场巨大。 ----在家可经营所有国家生意,事业规模宏大。 ----不需求人与说服;不用放厚脸皮去推销。 ----极小投资;零风险;成...
    by jackielongteng(注册) on 2009/6/14 13:43:56
  • re:作用域
    <p>☆                    &deg;∵☆       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
    by jackielongteng(注册) on 2009/6/14 13:03:25
  • re:Html标签嵌套对展示性能的影响
    <p><strong>所有的浏览器都按照你提到的浏览器解析Html规则来解析嘛?</strong></p>
    by Cola(注册) on 2009/6/12 23:07:28
  • re:Html5
    <p>目前来说,HTML5还只是一个梦想,呵呵:)</p>
    by 开心就好(注册) on 2009/6/11 16:31:54
  • re:多线程与SqlConnection.Close
    <p>好服月租型IT服务台,与你共成长! 月租型ITSM软件,注册即可免费体验! 详情请登录官方网站:<a href="http://www.servicezon.co...
    by qzhibo(注册) on 2009/6/3 15:14:21
  • re:多线程Singleton单件模式
    <pre><span style="color: #0000ff;">//Another way public</span> <sp...
    by Yaojian(注册) on 2009/4/22 14:02:35
  • re:Thread.Sleep(0)
    <p>学习了~</p>
    by shuitong888(注册) on 2009/4/8 14:29:24
  • re:Html标签嵌套对展示性能的影响
    <p>DIV固然好 但IE6,7,8 firefox,safari ....做美工的人要累死.</p>
    by ryq1(注册) on 2009/4/3 14:16:25
  • re:用.net 编码实现朗读文本的方法
    <p>我第一次 按键时 能听到声音,但是第二次按键时,没反应。网页一直在 loading.&nbsp;是什么原因?</p>
    by tracytang949(注册) on 2009/3/27 7:01:09
  • re:information_schema.routines与sysobjects
    <p>用sys.procedures多好。</p>
    by luke(注册) on 2009/3/16 16:45:49
  • re:SQL Server 2005 配置发送邮件
    <p>&lt;A href="<a href="http://www.3rt.info">http://www.3rt.info</...
    by ives007(注册) on 2009/2/26 16:47:00
  • re:推荐 Gemini 这个bug管理工具
    <p>你好!首先非常感谢推荐使用Gemini,这段时间在使用Gemini,有些问题想请教以下。</p> <p>1.Create Issue 以后,设置了Visib...
    by CowboyRyan(注册) on 2009/2/20 15:45:08
  • re:推荐 Gemini 这个bug管理工具
    <p>你好!首先非常感谢推荐使用Gemini,这段时间在使用Gemini,有些问题想请教以下。</p> <p>1.Create Issue 以后,设置了Visib...
    by CowboyRyan(注册) on 2009/2/20 15:32:06
  • re:虚机搭配NLB负载平衡时碰到"没有接口可用于安装新的群集"的解决方案
    <p>google newsid</p>
    by iads(注册) on 2009/2/13 17:25:07
  • re:try catch 与线程
    <p>确实是这样的。因为异常机制本质上是堆栈操作,而各线程的堆栈是独立的。</p>
    by st_szr(注册) on 2009/1/21 9:46:05
  • re:try catch 与线程
    <p>没啥啊,线程就是新启动了一个,当然异常不会影响到原有的线程了。</p> <p>你应该在线程里面合适的位置写上自己的捕获代码就行了。</p>
    by laozizhu(注册) on 2009/1/19 16:33:21
  • re:我的2008,征服天堂
    <p>蝈蝈,可惜我帮不了你啊!</p>
    by laozizhu(注册) on 2009/1/19 16:25:45
  • re:try catch 与线程
    <p>呃&hellip;&hellip;是这样的。可怎么处理呢?</p>
    by Anders Liu(注册) on 2009/1/19 11:58:05
  • re:我的2008,征服天堂
    <p>博主是不是去了师部 做了侦查营长呢?</p>
    by huobazi(注册) on 2009/1/9 14:15:33
  • re:我的2008,征服天堂
    <p>@ghj1976:看来真的危机了</p>
    by 开心就好(注册) on 2009/1/9 10:17:37
  • re: 网络带宽的单位
    不过传输的时候,往往还有压缩。
    by luke(匿名) on 2008/12/15 11:00:21
  • re: 网络带宽的单位
    除10不仅仅是为了方便,在传输中,往往加上控制位,所以一个字节往往需要10Bit.
    by 关门放狗(匿名) on 2008/12/13 16:01:30
  • re: 多缓存并存
    对跨进程甚至跨服务器缓存的性能比较怀疑,进程通信和跨服务器通信代价不菲。即使有已有进程外数据可用,如果考虑在进程做份缓存,定期再进程间同步是否更佳?
    by jinglecat(匿名) on 2008/12/12 18:00:05
  • re: 网络带宽的单位
    好像还有一个为了方便换算,厂家使用的是 除10的处理方式的说法:于是100Mb/sec = 100M / 10 = 10M Byte/sec 所以我通常都是用除10而不是除8来做运算的。
    by kentliu(匿名) on 2008/12/11 11:38:55
  • re: 网络带宽的单位
    又不是大S小s
    by luke(匿名) on 2008/12/10 12:04:50

广告

 

先看一个现象:
如果你有这样的一个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.1.0