5

既存のプロジェクトでコード分析を実行しているときに、メッセージに出くわしまし。ただし、このクラスは、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);
            }
        }
    }
4

2 に答える 2

5

どうですか:

/// <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; private set; }

  /// <summary>
  /// Gets or sets the list of IPG prefixes.
  /// </summary>
  [XmlArray("IpgPrefixes")]
  [XmlArrayItem("IpgPrefix")]
  public Collection<string> IpgPrefixes { get; private set; }

  public ConfigurationStorage() {
    Executers = new Collection<string>();
    IpgPrefixes = new Collection<string>();
  }
}

これは、xml シリアライゼーション/デシリアライゼーションでも機能します。

于 2013-08-08T08:58:31.117 に答える
0

MSDNのドキュメントを読むと、次のメモが表示されます。

XmlSerializer は、IEnumerable または ICollection を実装するクラスを特別に扱います。IEnumerable を実装するクラスは、1 つのパラメーターを受け取るパブリックな Add メソッドを実装する必要があります。Add メソッドのパラメーターは、GetEnumerator から返される値の Current プロパティから返されるものと同じ型、またはその型のベースの 1 つでなければなりません。IEnumerable に加えて ICollection (CollectionBase など) を実装するクラスには、整数を受け取るパブリック Item インデックス プロパティ (C# のインデクサー) と、整数型のパブリック Count プロパティが必要です。Add メソッドのパラメーターは、Item プロパティから返される型と同じ型か、その型のベースの 1 つでなければなりません。ICollection を実装するクラスの場合、シリアル化される値は、インデックス付きの Item プロパティから取得されます。

XmlSerializerしたがって、この特別な扱いに沿うと、 で動作し、従来の名前空間を使用せず、正しい方法でコード分析の警告を満たすより良いコードが得られると思います。ルール。

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

/// <summary>
/// Class containing the Configuration Storage
/// </summary>
[XmlRoot("Configuration")]
public class ConfigurationStorage
{
    // The executers.
    private readonly ICollection<string> executers = new List<string>();

    // The IPG prefixes.
    private readonly ICollection<string> ipgPrefixes = new List<string>();

    /// <summary>
    /// Gets the list of executers.
    /// </summary>
    [XmlArray("Executers")]
    [XmlArrayItem("Executer")]
    public ICollection<string> Executers
    { 
        get
        {
            return this.executers;
        }
    }

    /// <summary>
    /// Gets the list of IPG prefixes.
    /// </summary>
    [XmlArray("IpgPrefixes")]
    [XmlArrayItem("IpgPrefix")]
    public ICollection<string> IpgPrefixes
    { 
        get
        {
            return this.ipgPrefixes;
        }
    }
}
于 2013-08-08T09:19:34.320 に答える