我正在编写一个简单的解析器,它将采用20100101,1,2,foo格式的字符串并创建以下类的实例:
public class Foo
{
public DateTime TheDate { get; set; }
public int TheFirstInt { get; set; }
public int TheSecondInt { get; set; }
public string TheString { get; set; }
}我希望能够将每个属性的解析器声明为(例如) Func<>s数组,以使代码更加可读性(从字符串中的项与所使用的解析代码相关联的角度来看)。
// Production code would contain parsers with error checking etc.
Func<string, object>[] parsers = new Func<string, object>[]
{
s => DateTime.ParseExact(s, "yyyyMMdd", CultureInfo.InvariantCulture),
s => int.Parse(s),
s => int.Parse(s),
s => s
};然后,我希望能够在一个循环中迭代FooClass的解析器、属性和fooItems中的值:
Foo fooInstance = new Foo();
string[] fooItems = fooString.Split(',');
for (int i = 0; i < parsers.Length; i++)
{
fooInstance.Properties[i] = parsers[i](fooItems[i]);
// range-checking and error handling excluded from this example
}但是,这当然不能工作,因为:
fooInstance
值的转换
对于如何写这样一个“愉快”的解析器,有什么想法吗?
发布于 2010-02-01 15:46:03
我尝试使用Actions而不是Funcs,并直接设置属性:
Action<string, FooClass>[] actions = new Action<string, FooClass>[] {
(s, c) => c.TheDate = DateTime.ParseExact(s, "yyyyMMdd", CultureInfo.InvariantCulture),
(s, c) => c.TheFirstInt = Int32.Parse(s)
// ...
}
for (int i = 0; i < fooItems.Length; ++i)
actions[i](fooItems[i], fooInstance);发布于 2010-02-01 16:05:15
我知道这不是直接回答你的问题,但如果你发现你的“语言”变得复杂得多,那么我建议你用反讽来解析它:http://www.codeplex.com/irony
如果您的语言将保持平面格式(如CSV),那么值得一看http://www.filehelpers.com/
在您的示例中,只需注释您的类:
[DelimitedRecord(",")]
public class Foo
{
[FieldConverter(ConverterKind.Date, "yyyyMMdd")]
public DateTime TheDate { get; set; }
public int TheFirstInt { get; set; }
public int TheSecondInt { get; set; }
public string TheString { get; set; }
}然后用以下方法解析它:
FileHelperEngine engine = new FileHelperEngine(typeof(Foo));
Foo[] fs = engine.ReadFile("FileIn.txt") as Foo[];发布于 2010-02-01 15:38:30
你需要使用反射:
fooInstance.GetType().GetProperty("SomeProp").SetValue(fooInstance, "SomeProp", val);https://stackoverflow.com/questions/2177786
复制相似问题