简体   繁体   English

Sonar Qube Error 更新“ISerializable”的实现以符合推荐的序列化模式

[英]Sonar Qube Error Update this implementation of 'ISerializable' to conform to the recommended serialization pattern

I'm currently working on a .net 4.6.2 application.我目前正在处理 .net 4.6.2 应用程序。

I need to serialize an OData Api call and it works perfectly fine.我需要序列化一个 OData Api 调用,它工作得很好。

Unfortunately I'm getting a Sonar Qube Error:不幸的是,我遇到了 Sonar Qube 错误:

Update this implementation of 'ISerializable' to conform to the recommended serialization pattern.更新“ISerializable”的此实现以符合推荐的序列化模式。

在此处输入图像描述

To get my OData into C#, I use the following class structure:为了将我的 OData 放入 C#,我使用以下 class 结构:

[Serializable]
public class Record : Dictionary<string, dynamic> { }

[DataContract]
public class Records
{
    [DataMember(Name = "@odata.context")]
    public string Context { get; set; }

    [DataMember(Name = "@odata.count")]
    public int Count { get; set; }

    [DataMember(Name = "value")]
    public IEnumerable<Record> Value { get; set; }
}

The serialization works fine, but I don't know how to solve this Sonar Qube error.序列化工作正常,但我不知道如何解决这个 Sonar Qube 错误。

How to properly use ISerializable together with DataContract, is it actually possible?如何正确使用 ISerializable 和 DataContract,真的可以吗?

Do you know how to solve this issue?你知道如何解决这个问题吗?

As suggested by @Maritn Costello正如@Maritn Costello 所建议的

you could suppress this warning like this.你可以像这样抑制这个警告。

#pragma warning disable S3925 // "ISerializable" should be implemented correctly
    public class Record : Dictionary<string, string> { }
#pragma warning restore S3925 // "ISerializable" should be implemented correctly

Dictionary class implement ISerializable字典 class 实现ISerializable

 public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, ISerializable, IDeserializationCallback
    {

In my case I chose to use the native type instead of a custom class. It works fine.在我的例子中,我选择使用本机类型而不是自定义 class。它工作正常。

[DataMember(Name = "value")]
public List<Dictionary<string, dynamic>> Value { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM