【问题标题】:Why a class does not implement ISerializable interface violates MissingSerializationConstructorRule为什么一个类不实现 ISerializable 接口违反 MissingSerializationConstructorRule
【发布时间】:2012-10-13 10:22:17
【问题描述】:

我们有声纳报告说我们项目中的许多类违反了MissingSerializationConstructorRule,但是该类及其基类都没有实现任何 Iserializable 接口,有人知道为什么吗?

例如,声纳说:

public class CommentPage : RmdsPublicationPage, ICommentPage 
    { 
 *MissingSerializationConstructorRule    
 The required constructor for ISerializable is not present in this type.* 
    public CommentPage() 
    { 
        this["COMMENTTXT"] = null; 

对应的类在哪里

public class CommentPage : RmdsPublicationPage, ICommentPage
{
    public CommentPage()
    {
        // do something
    }
    public void Update(string comment)
    {
        //something else
    }
}

两个接口也没有实现 ISerializable,即

public class RmdsPublicationPage : Dictionary<string, object>, IRmdsPublicationPage

public interface IRmdsPublicationPage : IDictionary<string, object>, IDisposable

【问题讨论】:

  • ICommentPage 怎么样?你还没有发布。
  • 任何从 ISerializable 派生的子类都必须实现特殊的构造函数,否则它们将无法正确反序列化。字典可以,你的孩子也应该从中学习。
  • 啊,谢谢你们,我在看 IDictionary 而不是字典。

标签: c# serializable


【解决方案1】:

Dictionary(TKey, TValue) 实现 ISerializable

[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, 
    ICollection<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, 
    IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, 
    IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, ISerializable, 
    IDeserializationCallback

【讨论】:

    猜你喜欢
    • 2011-04-24
    • 2013-05-24
    • 2014-09-16
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2011-03-14
    相关资源
    最近更新 更多