~myclass();_您必须在MyClass上实现Add(System.Object)方法,因为它继承自IEnumerable

~myclass();

~myclass();

Peter was getting the error "You must implement the Add(System.Object) method on MyClass because it inherits from IEnumerable" while serializing an object. He added an Add() method and the error went away, but he was rightfully confused by this odd message because IEnumerable doesn't require an Add as part of its contract! The error message arguably uses the word "implement" in a way that implies something about the interface in question.

Peter在序列化对象时遇到错误“您必须在MyClass上实现Add(System.Object)方法,因为它继承自IEnumerable ”。 他添加了一个Add()方法,错误消失了,但是他理所当然地对这个奇怪的消息感到困惑,因为IEnumerable不需要将Add作为合同的一部分! 错误消息可以说是用“实现”一词暗示某种有关接口的方式。

Remember that the XmlSerializer has to serialize and deserialize, and does so only via public means. For something that implements IEnumerable, there'd be no way to get objects back in without something like an Add.

请记住,XmlSerializer必须序列化反序列化,并且只能通过公共方式进行。 对于一些实现IEnumerable,有好多是没有办法让对象,而回来的东西一个Add。

Sairama pointed to this paragraph buried in the MSDN docs:

Sairama指出了MSDN文档中隐藏的这一段

The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type's bases. A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item indexed property (indexer in C#) that takes an integer, and it must have a public Count property of type integer. The parameter to the Add method must be the same type as is returned from the Item property, or one of that type's bases. For classes implementing ICollection, values to be serialized will be retrieved from the indexed Item property, not by calling GetEnumerator

XmlSerializer对实现IEnumerable或ICollection的类给予特殊待遇。 实现IEnumerable的类必须实现采用单个参数的公共Add方法。 Add方法的参数必须与GetEnumerator返回的值的Current属性所返回的类型相同,或该类型的基数之一。 除了IEnumerable之外,实现ICollection的类(例如CollectionBase)必须具有带整数的公共Item索引属性(在C#中为indexer),并且必须具有整数类型的公共Count属性。 Add方法的参数必须与Item属性返回的参数类型相同,或者是该类型的基础之一。 对于实现ICollection的类,将从索引的Item属性中检索要序列化的值,而不是通过调用GetEnumerator

Now playing: Ladysmith Black Mambazo - N'kosi Sikeleli Afrika (God Bless Africa)

现在播放: Ladysmith Black Mambazo - N'kosi Sikeleli Afrika(上帝保佑非洲)

翻译自: https://www.hanselman.com/blog/you-must-implement-the-addsystemobject-method-on-myclass-because-it-inherits-from-ienumerable

~myclass();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值