既存のプロジェクトでコード分析を実行しているときに、メッセージに出くわしました。ただし、このクラスは、xml 構成ファイルの読み取り/書き込みに使用されます。このクラスをCA1002およびCA2227に準拠させることは可能ですか、それともXML 関連のクラス (プロジェクトには多くのクラスがあります) のこれらの規則を抑制する必要がありますか?
編集
解決済みの CA1002に変更List<string>
します。Collection<string>
CA2227 を解決し、全体を (逆) シリアル化する方法についてはまだ手がかりがありません。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
/// <summary>
/// Class containing the Configuration Storage
/// </summary>
[XmlRoot("Configuration")]
public class ConfigurationStorage
{
/// <summary>
/// Gets or sets the list of executers.
/// </summary>
[XmlArray("Executers")]
[XmlArrayItem("Executer")]
public Collection<string> Executers { get; set; }
/// <summary>
/// Gets or sets the list of IPG prefixes.
/// </summary>
[XmlArray("IpgPrefixes")]
[XmlArrayItem("IpgPrefix")]
public Collection<string> IpgPrefixes { get; set; }
}
xml ファイルの読み取り:
public static ConfigurationStorage LoadConfiguration()
{
if (File.Exists(ConfigFile))
{
try
{
using (TextReader r = new StreamReader(ConfigFile))
{
var s = new XmlSerializer(typeof(ConfigurationStorage));
var config = (ConfigurationStorage)s.Deserialize(r);
return config;
}
}
catch (InvalidOperationException invalidOperationException)
{
throw new StorageException(
"An error occurred while deserializing the configuration XML file.", invalidOperationException);
}
}
}