Net Core:静态类需要 SonarQube 的 ISerialized 吗?

有人理解 Sonarqube 中的这个错误吗?我们想要实现一个网格需求维度——给定一定数量的图片。2张图片是1X2网格,4张图片是2X2,6张图片是3X2等等。Sonarqube要求为这种简单的静态类实现Serialized。我该如何解决这个问题,有必要吗?


当前代码:


public class CardDimensionRequirement:Dictionary<int, CardDimensionRequirementLine> 

{

    public void AddItem(int PictureCount, int Length, int Height, int BootstrapDimension)

    {

        base.Add(PictureCount, new CardDimensionRequirementLine(PictureCount, Length, Height, BootstrapDimension));

    }


    public int GetMaxKey()

    {

        return base.Keys.Max();

    }    

}


static class CardDimensionRequirementSpecs

{

    public static CardDimensionRequirement cardDimensionRequirementData;


    static CardDimensionRequirementSpecs()

    {

        //int PictureCount, int Length, int Height, int BootstrapDimension

        cardDimensionRequirementData = new CardDimensionRequirement();

        cardDimensionRequirementData.AddItem(1, 1, 1, 1);

        cardDimensionRequirementData.AddItem(2, 2, 1, 6);

        cardDimensionRequirementData.AddItem(3, 3, 1, 4);

        cardDimensionRequirementData.AddItem(4, 2, 2, 6);

        cardDimensionRequirementData.AddItem(5, 3, 2, 4);

        cardDimensionRequirementData.AddItem(6, 3, 2, 4);

    }

}

https://img2.mukewang.com/64fc2c060001570a06530255.jpg

杨__羊羊
浏览 76回答 1
1回答

慕标琳琳

SonarQube 可能会给出该错误,因为您的基本类型 ,Dictionary<TKey,TValue>通过接口实现自定义二进制序列化ISerializable。鉴于您的基类型实现了自定义序列化,SonarQube 似乎假设您需要覆盖该自定义序列化以添加派生类型的声明成员的序列化。但是,您的类型没有声明自己的字段或属性,因此没有任何特定于序列化的内容。那么,您有什么选择来解决这个问题?如果您不关心二进制序列化并且不需要支持它,您可以采用装饰器模式并实现IDictionary<int, CardDimensionRequirementLine>,而不是从Dictionary<TKey,TValue>. 然后,里面CardDimensionRequirement有一些私有字典来进行实际的查找:public class CardDimensionRequirement : IDictionary<int, CardDimensionRequirementLine>{    readonly Dictionary<int, CardDimensionRequirementLine> dictionary = new Dictionary<int, CardDimensionRequirementLine>();    public void AddItem(int PictureCount, int Length, int Height, int BootstrapDimension)    {        Add(PictureCount, new CardDimensionRequirementLine(PictureCount, Length, Height, BootstrapDimension));    }    public int GetMaxKey()    {        return Keys.Max();    }       #region IDictionary<int,CardDimensionRequirementLine> Members    // Delegate everything to this.dictionary:    public void Add(int key, CardDimensionRequirementLine value)    {        this.dictionary.Add(key, value);    }    // Remainder snipped不要将该类标记为[Serializable]或实现ISerializable。此实现的优点是切换到不同的字典(例如 )SortedDictionary<int, CardDimensionRequirementLine>不会是重大更改。如果您确实关心二进制序列化,您应该将您的类型标记为[Serializable]、 override GetObjectData(),并引入您自己的流式构造函数,如下所示:[Serializable]public class CardDimensionRequirement : Dictionary<int, CardDimensionRequirementLine>{    public CardDimensionRequirement() : base() { }    protected CardDimensionRequirement(SerializationInfo info, StreamingContext context)        : base(info, context)    {        // Nothing to do since your class currently has no fields    }    public override void GetObjectData(SerializationInfo info, StreamingContext context)    {        base.GetObjectData(info, context);        // Deserialize fields here, if you ever add any.    }    // Remainder snipped作为替代方案,由于您CardDimensionRequirement实际上没有任何自己的数据需要记住,您可以简单地使用任何旧的Dictionary<int, CardDimensionRequirementLine>方法并将您的方法实现为扩展方法:public static class CardDimensionRequirementExtensions{    public static void AddItem(this IDictionary<int, CardDimensionRequirementLine> dictionary, int PictureCount, int Length, int Height, int BootstrapDimension)    {        if (dictionary == null)            throw new ArgumentNullException();        dictionary.Add(PictureCount, new CardDimensionRequirementLine(PictureCount, Length, Height, BootstrapDimension));    }    public static int GetMaxKey(this IDictionary<int, CardDimensionRequirementLine> dictionary)    {        if (dictionary == null)            throw new ArgumentNullException();        return dictionary.Keys.Max();    }   }
打开App,查看更多内容
随时随地看视频慕课网APP