2

XML ファイルを読み取る必要があり、LINQ to XML アプローチを使用しています。クラスのインスタンスを作成するときIgnoreに問題がありmodeます。クラスpatternのコンストラクターで定義されたデフォルト値に属性を設定する必要がある場合、属性を XML に含める必要がないからです。Ignore

以下のコードは機能しますが、すべての属性が XML ファイルに存在する場合に限られます。

var items = xmlFile.Descendants("item")
                   .Select(x => new Item()
                       {
                           SourcePath = x.Attribute("source").ToString(),
                           DestinationPath = x.Attribute("destination").ToString(),
                           IgnoredSubitems = new List<Ignore>(
                              x.Elements("ignore")
                               .Select(y => new Ignore(
                                   path: y.Value,
                                   mode: GetEnumValue<IgnoreMode>(y.Attribute("mode")),
                                   pattern: GetEnumValue<IgnorePattern>(y.Attribute("style"))
                                ))
                            )
                        })
                    .ToList();

GetEnumValue列挙型を設定するために使用されるメソッドは次のようになります

private static T GetEnumValue<T>(XAttribute attribute)
{
    return (T)Enum.Parse(typeof(T), attribute.ToString());
}

値がある場合にのみフィールドを設定する方法はありますか?それ以外の場合は、コンストラクターで定義されたデフォルト値を使用しますか? Ignoreクラスは不変である必要があるため、最初にデフォルト値でインスタンス化してから、ゲッターのみを提供するプロパティに値を割り当てようとすることはできません。

編集:

誤解された回答に基づいています。クラスは次のIgnoreようになります。私のクラスではないことに注意してください。

public class Ignore
{
    string Path { get; }
    IgnoreMode Mode { get; } // enum
    IgnorePattern Pattern { get; } // enum

    public Ignore(string path, IgnoreMode mode = someDefaultValue, IgnorePattern pattern = someDefaultPattern) 
    { 
        ... I don't know what happens here, but I guess that arguments are assigned to the properties ... 
    }
}

デフォルト値は時間の経過とともに変化する可能性があり、ローダーでハードコードすることはできません。

4

2 に答える 2

1

このようなリフレクションを使用できます

var constructor = typeof(Ignore).GetConstructor(new Type[]{typeof(string),typeof(IgnoreMode),typeof(IgnorePattern)});
var parameters = constructor.GetParameters(); // this return list parameters of selected constructor
var defaultIgnoreMode = (IgnoreMode)parameters[1].DefaultValue;
var defaultIgnorePattern = (IgnorePattern)parameters[2].DefaultValue;
于 2013-10-16T10:12:41.287 に答える
0

文字列パラメータを GetEnumValue メソッドに渡します

var items = xmlFile.Descendants("item")
                   .Select(x => new Item()
                       {
                           SourcePath = x.Attribute("source").ToString(),
                           DestinationPath = x.Attribute("destination").ToString(),
                           IgnoredSubitems = new List<Ignore>(
                              x.Elements("ignore")
                               .Select(y => new Ignore(
                                   path: y.Value,
                                   mode: GetEnumValue<IgnoreMode>((string)y.Attribute("mode")),
                                   pattern: GetEnumValue<IgnorePattern>((string)y.Attribute("style"))
                                ))
                            )
                        })
                    .ToList();

このようにメソッドを変更します

private static T GetEnumValue<T>(string attribute)
{
    if(attribute==null) return YOURDEFAULTVALUE;
    else return (T)Enum.Parse(typeof(T), attribute);
}

それがあなたのために働くことを願っています

于 2013-10-16T09:19:00.443 に答える