我有生成的实体类(如MyClass),我需要读取一个csv文件,然后返回MyClass的列表。 在所有实体上,我们都有一个 readonly 属性 CreateOn。 要设置 CreatedOn,我们需要执行以下自定义代码: Attributes["overriddencreatedon"] = DateTime.Now;
CsvHelper 可以做这样的事情吗? :
public class MyClass // in real life, inherits from Microsoft.Xrm.Sdk.Entity
{
public Guid Id { get; set; }
public DateTime CreatedOn { get;} // Readonly because generated class...
public Dictionary<string, object> Attributes { get; set; }
}
private class MyClassMap : ClassMap<MyClass>
{
public MyClassMap()
{
Map(f => f.Id).Name("Id");
// Cannot do this : Map(f => f.CreatedOn).Name("CreatedOn");
// Because CreatedOn is readonly
// The goal is to store the value in the dictionary Attributes, not in the property CreatedOn because it is readonly
// Instead, we need to do something like this:
Map().XXX((row, x) => x.Attributes["overriddencreatedon"] = row.Row.GetField<DateTime>("CreatedOn"));
}
}
这应该有效。
Map(f => f.Attributes).Convert(args =>
{
return new Dictionary<string, object> {
{"overriddencreatedon", args.Row.GetField<DateTime>("CreatedOn") }
};
});