1 回答

TA貢獻1830條經驗 獲得超9個贊
SonarQube 可能會給出該錯誤,因為您的基本類型 ,Dictionary<TKey,TValue>
通過接口實現(xiàn)自定義二進制序列化ISerializable
。鑒于您的基類型實現(xiàn)了自定義序列化,SonarQube 似乎假設您需要覆蓋該自定義序列化以添加派生類型的聲明成員的序列化。
但是,您的類型沒有聲明自己的字段或屬性,因此沒有任何特定于序列化的內容。
那么,您有什么選擇來解決這個問題?
如果您不關心二進制序列化并且不需要支持它,您可以采用裝飾器模式并實現(xiàn)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]
或實現(xiàn)ISerializable
。
此實現(xiàn)的優(yōu)點是切換到不同的字典(例如 )SortedDictionary<int, CardDimensionRequirementLine>
不會是重大更改。
如果您確實關心二進制序列化,您應該將您的類型標記為[Serializable]
、 override?GetObjectData()
,并引入您自己的流式構造函數(shù),如下所示:
[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實際上沒有任何自己的數(shù)據(jù)需要記住,您可以簡單地使用任何舊的Dictionary<int, CardDimensionRequirementLine>方法并將您的方法實現(xiàn)為擴展方法:
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();
? ? }? ?
}
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報