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的,因此我不能先用默认值实例化它,然后尝试将值分配给仅提供 getter 的属性。

编辑:

基于误解的答案。该类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 回答