英文:
Cannot write external object inside of custom object to CSV using CSVHelper with ClassMap
问题
我想将Vector3对象(参见https://docs.unity3d.com/ScriptReference/Vector3.html)写入我的自定义类的CSV文件中:
public class BehaviouralData
{
public float ActionZ { get; set; }
public float ActionX { get; set; }
public int TargetBallAgentHashCode { get; set; }
public Vector3 TargetBallLocalPosition { get; set; }
public bool CollectBehaviouralData { get; set; }
public DateTime ActionTime { get; set; }
public DateTime Time { get; set; }
}
因此,我使用了一个ClassMap:
public sealed class Vector3Map : ClassMap<Vector3>
{
public Vector3Map()
{
Map(m => m.x);
Map(m => m.y);
Map(m => m.z);
}
}
在写入文件之前,我要注册它:
...
using (var csv = new CsvWriter(writer, config))
{
csv.Context.RegisterClassMap<Vector3Map>();
csv.WriteRecords(data);
}
...
当我尝试保存文件时,我收到以下错误消息:
ArgumentException: Incorrect number of arguments supplied for call to method 'Single get_Item(Int32)'
Parameter name: property
System.Linq.Expressions.Expression.Property (System.Linq.Expressions.Expression expression, System.Reflection.PropertyInfo property) (at <1e18c5a6594041c9844bfd0b6618ee4a>:0)
CsvHelper.Expressions.ExpressionManager.CreateGetMemberExpression (System.Linq.Expressions.Expression recordExpression, CsvHelper.Configuration.ClassMap mapping, CsvHelper.Configuration.MemberMap memberMap) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.ExpressionManager.CreateGetMemberExpression (System.Linq.Expressions.Expression recordExpression, CsvHelper.Configuration.ClassMap mapping, CsvHelper.Configuration.MemberMap memberMap) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.ObjectRecordWriter.CreateWriteDelegate[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordWriter.GetWriteDelegate[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordWriter.Write[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordManager.Write[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.CsvWriter.WriteRecords[T] (System.Collections.Generic.IEnumerable`1[T] records) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
Rethrow as WriterException: An unexpected error occurred.
IWriter state:
Row: 2
Index: 0
HeaderRecord:
2
CsvHelper.CsvWriter.WriteRecords[T] (System.Collections.Generic.IEnumerable`1[T] records) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
Util.SaveDataToCSV[T] (System.String path, System.Collections.Generic.List`1[T] data) (at Assets/Scripts/Util/Util.cs:178)
BehaviourMeasurement.SaveRawBehavioralDataToCSV () (at Assets/Scripts/Measurement/BehaviourMeasurementBehaviour.cs:1021)
BehaviourMeasurementBehaviour.OnDisable () (at Assets/Scripts/Measurement/BehaviourMeasurementBehaviour.cs:122)
而空的CSV文件具有以下列:
ActionZ,ActionX,TargetBallAgentHashCode,Item,Item,magnitude,sqrMagnitude,magnitude,sqrMagnitude,CollectBehaviouralData,ActionTime,Time
正如您所见,文件中没有写入坐标,而是其他属性(例如magnitude
),我不想包括它们。为什么CSVHelper没有将正确的属性映射到CSV文件中?
英文:
I want to write a Vector3 object (see https://docs.unity3d.com/ScriptReference/Vector3.html) which is part of my custom class to a CSV file:
public class BehaviouralData
{
public float ActionZ { get; set; }
public float ActionX { get; set; }
public int TargetBallAgentHashCode { get; set; }
public Vector3 TargetBallLocalPosition { get; set; }
public bool CollectBehaviouralData { get; set; }
public DateTime ActionTime { get; set; }
public DateTime Time { get; set; }
}
Therefore I am using a ClassMap
public sealed class Vector3Map : ClassMap<Vector3>
{
public Vector3Map()
{
Map(m => m.x);
Map(m => m.y);
Map(m => m.z);
}
}
Which I register before writing to a file:
...
using (var csv = new CsvWriter(writer, config))
{
csv.Context.RegisterClassMap<Vector3Map>();
csv.WriteRecords(data);
}
...
When I try to save the file, I get the following error message:
ArgumentException: Incorrect number of arguments supplied for call to method 'Single get_Item(Int32)'
Parameter name: property
System.Linq.Expressions.Expression.Property (System.Linq.Expressions.Expression expression, System.Reflection.PropertyInfo property) (at <1e18c5a6594041c9844bfd0b6618ee4a>:0)
CsvHelper.Expressions.ExpressionManager.CreateGetMemberExpression (System.Linq.Expressions.Expression recordExpression, CsvHelper.Configuration.ClassMap mapping, CsvHelper.Configuration.MemberMap memberMap) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.ExpressionManager.CreateGetMemberExpression (System.Linq.Expressions.Expression recordExpression, CsvHelper.Configuration.ClassMap mapping, CsvHelper.Configuration.MemberMap memberMap) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.ObjectRecordWriter.CreateWriteDelegate[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordWriter.GetWriteDelegate[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordWriter.Write[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.Expressions.RecordManager.Write[T] (T record) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
CsvHelper.CsvWriter.WriteRecords[T] (System.Collections.Generic.IEnumerable`1[T] records) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
Rethrow as WriterException: An unexpected error occurred.
IWriter state:
Row: 2
Index: 0
HeaderRecord:
2
CsvHelper.CsvWriter.WriteRecords[T] (System.Collections.Generic.IEnumerable`1[T] records) (at <9a1379e089274cc1baf4edcf480e4c6d>:0)
Util.SaveDataToCSV[T] (System.String path, System.Collections.Generic.List`1[T] data) (at Assets/Scripts/Util/Util.cs:178)
BehaviourMeasurement.SaveRawBehavioralDataToCSV () (at Assets/Scripts/Measurement/BehaviourMeasurementBehaviour.cs:1021)
BehaviourMeasurementBehaviour.OnDisable () (at Assets/Scripts/Measurement/BehaviourMeasurementBehaviour.cs:122)
And the empty CSV file has the following columns:
ActionZ,ActionX,TargetBallAgentHashCode,Item,Item,magnitude,sqrMagnitude,magnitude,sqrMagnitude,CollectBehaviouralData,ActionTime,Time
As you can see not the coordinates are written to the file but other properties (e.g. magnitude
) which I do not want to include.
Why does CSVHelper not map the right properties into the CSV file?
答案1
得分: 2
我在 .Net Fiddle 中进行了一些尝试。
当为 BehaviouralData
使用显式映射时,似乎问题已经解决。
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
Map(o => o.ActionZ);
Map(o => o.ActionX);
Map(o => o.TargetBallAgentHashCode);
Map(o => o.TargetBallLocalPosition);
Map(o => o.CollectBehaviouralData);
Map(o => o.ActionTime);
Map(o => o.Time);
}
}
当使用自动映射时,可能默认情况下会使用以下方式,但问题再次出现。
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
AutoMap(CultureInfo.InvariantCulture);
}
}
也尝试了以下方式,但问题相同。
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
AutoMap(CultureInfo.InvariantCulture);
Map(o => o.TargetBallLocalPosition).Ignore();
Map(o => o.TargetBallLocalPosition.x);
Map(o => o.TargetBallLocalPosition.y);
Map(o => o.TargetBallLocalPosition.z);
}
}
更新
实际上,这种显式映射也不起作用,只会将 Vector3
打印到 CSV 中,因此第二个映射似乎完全被忽略了。
以下方式似乎能够工作,尽管看起来相当显式,最多只能算是一种解决方法。
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
Map(o => o.ActionZ);
Map(o => o.ActionX);
Map(o => o.TargetBallAgentHashCode);
Map(o => o.TargetBallLocalPosition.x);
Map(o => o.TargetBallLocalPosition.y);
Map(o => o.TargetBallLocalPosition.z);
Map(o => o.CollectBehaviouralData);
Map(o => o.ActionTime);
Map(o => o.Time);
}
}
但是老实说,至少对于写入部分来说,这似乎非常显式,最多只能算是一种解决方法。
英文:
I played around a bit in a .Net Fiddle.
When using an explicit map also for the BehaviouralData
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
Map(o => o.ActionZ);
Map(o => o.ActionX);
Map(o => o.TargetBallAgentHashCode);
Map(o => o.TargetBallLocalPosition);
Map(o => o.CollectBehaviouralData);
Map(o => o.ActionTime);
Map(o => o.Time);
}
}
it seemed to be fixed.
When using automapping like
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
AutoMap(CultureInfo.InvariantCulture);
}
}
which is probably what is going to be used by default anyway - it broke again.
Also tried this but same
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
AutoMap(CultureInfo.InvariantCulture);
Map(o => o.TargetBallLocalPosition).Ignore();
Map(o => o.TargetBallLocalPosition.x);
Map(o => o.TargetBallLocalPosition.y);
Map(o => o.TargetBallLocalPosition.z);
}
}
Update
Actually this explicit mapping didn't work either -_-
It only printed Vector3
into the CSV so the second map seemed to be completely ignored.
Quite hideous but this seems to work
public class BehaviouralDataMap : ClassMap<BehaviouralData>
{
public BehaviouralDataMap()
{
Map(o => o.ActionZ);
Map(o => o.ActionX);
Map(o => o.TargetBallAgentHashCode);
Map(o => o.TargetBallLocalPosition.x);
Map(o => o.TargetBallLocalPosition.y);
Map(o => o.TargetBallLocalPosition.z);
Map(o => o.CollectBehaviouralData);
Map(o => o.ActionTime);
Map(o => o.Time);
}
}
BUT to be honest at least for the writer part this seems extremely explicit and at best would be a workaround.
答案2
得分: 1
您的基本问题是,正如derHugo在这个答案中指出的,当CsvHelper自动映射类型时,它会自动映射所有属性值的类型,而不是使用先前为这些类型注册的类映射。具体来说,在Vector3
的情况下,它会自动映射其属性,而不是字段 x
、y
和 z
,最终导致崩溃。<sup>[1]</sup>。
那么,有哪些解决方法呢?除了手动映射 x
、y
和 z
字段如derHugo的答案中建议的那样,您还有一些其他选项。
首先,您可以自动映射包含 Vector3
的类型,然后在之后修复引用。为此,请定义以下通用的 ClassMap<T>
和相关的扩展方法:
public class CustomAutoMap<TClass> : ClassMap<TClass>
{
// 自动映射给定的类型,然后修复对 Vector3 的引用(如果有)。
public CustomAutoMap()
{
AutoMap(CultureInfo.InvariantCulture);
// 现在修复 Vector3 的引用。
this.UpdateReferences<Vector3Map, Vector3>();
}
}
public static partial class CsvExtensions
{
// 更新自动映射的引用映射到不同的映射。
// 请注意,这只应在 ClassMap 构造函数内部执行。
public static ClassMap UpdateReferences<TReferenceMap, TReferenceClass>(this ClassMap classMap, params object[] constructorArgs) where TReferenceMap : ClassMap<TReferenceClass>
{
// TODO:嵌套引用的引用。
for (int i = 0, count = classMap.ReferenceMaps.Count; i < count; i++)
if (classMap.ReferenceMaps[i].Data.Member.MemberType() == typeof(TReferenceClass))
classMap.UpdateReference<TReferenceMap, TReferenceClass>(i, constructorArgs);
return classMap;
}
public static MemberReferenceMap UpdateReference<TReferenceMap, TReferenceClass>(this ClassMap classMap, int index, params object[] constructorArgs) where TReferenceMap : ClassMap<TReferenceClass>
{
if (index < 0 || index >= classMap.ReferenceMaps.Count)
throw new ArgumentException(nameof(index));
var oldRef = classMap.ReferenceMaps[index];
classMap.ReferenceMaps.RemoveAt(index);
var newRef = classMap.References(typeof(TReferenceMap), oldRef.Data.Member, constructorArgs);
if (oldRef.Data.Prefix != null)
newRef.Prefix(oldRef.Data.Prefix);
classMap.ReferenceMaps.Swap(index, classMap.ReferenceMaps.IndexOfWithHint(newRef, classMap.ReferenceMaps.Count - 1));
return newRef;
}
static Type? MemberType(this MemberInfo m) =>
m switch
{
PropertyInfo p => p.PropertyType,
FieldInfo f => f.FieldType,
_ => null,
};
static int IndexOfWithHint<T>(this IList<T> list, T item, int hint) where T : class
{
if (hint >= 0 && hint < list.Count && list[hint] == item)
return hint;
return list.IndexOf(item);
}
static void Swap<T>(this IList<T> list, int i, int j)
{
if (i != j)
{
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
现在,将您的 BehaviouralData
序列化如下:
var config = new CsvConfiguration(CultureInfo.InvariantCulture);
using (var csv = new CsvWriter(writer, config))
{
csv.Context.RegisterClassMap<CustomAutoMap<BehaviouralData>>();
csv.WriteRecords(data);
}
这将产生以下结果:
ActionZ,ActionX,TargetBallAgentHashCode,CollectBehaviouralData,ActionTime,Time,x,y,z
0,0,1,False,01/01/0001 00:00:00,01/01/0001 00:00:00,1.1,1.1,1.1
0,0,2,False,01/01/0001 00:00:00,01/01/0001 00:00:00,2.1,2.1,2.1
0,0,3,False,01/01/0001 00:00:00,01/01/0001 00:00:00,3.1,3.1,3.1
如果您希望 x
、y
和 z
字段具有一些前缀,请在 TargetBallLocalPosition
中添加 [HeaderPrefix("prefix")]
,如下所示:
public class BehaviouralData
{
[CsvHelper.Configuration.Attributes.HeaderPrefix("TargetBallLocation_")]
public Vector3 TargetBallLocalPosition { get; set; }
// 其余部分不变
}
这将得到以下结果:
ActionZ,ActionX,TargetBallAgentHashCode,CollectBehaviouralData,ActionTime,Time,TargetBallLocation_x,TargetBallLocation_y,TargetBallLocation_z
0,0,1,False,01/01/0001 00:00:00,01/01/0001 00:00:00,1.1,1.1,1.1
0,0,2,False,01/01/0001 00:00:00,01/01/0001 00:00:00,2.1,2.1,2.1
0,0,3,False,01/01/0001 00:00:00,01/01/0001 00:00:00,3.1,3.1,3.1
如果您需要更多对 BehaviouralData
格式的
英文:
Your basic problem is that, as derHugo noted in this answer, when CsvHelper automaps a type, it automaps the types of all property values, rather than using any class maps that might have been previously registered for those types. Specifically in the case of Vector3
it automaps its properties instead of the fields x
, y
and z
, eventually leading to a crash.<sup>[1]</sup>.
So, what are your workarounds? Other than manually mapping the x
, y
and z
fields manually as suggested in derHugo's answer, you have a couple of additional options.
Firstly, you could automap your types containing Vector3
and fix the references afterwards. To do this, define the following generic ClassMap<T>
and associated extension methods:
public class CustomAutoMap<TClass> : ClassMap<TClass>
{
// Automaps the given type then fixes up references to Vector3 (if any).
public CustomAutoMap()
{
AutoMap(CultureInfo.InvariantCulture);
// Now fix the Vector3 references.
this.UpdateReferences<Vector3Map, Vector3>();
}
}
public static partial class CsvExtensions
{
// Update autoampped reference maps to a different map.
// Note this should only be done within a ClassMap constructor itself.
public static ClassMap UpdateReferences<TReferenceMap, TReferenceClass>(this ClassMap classMap, params object[] constructorArgs) where TReferenceMap : ClassMap<TReferenceClass>
{
// TODO: nested references of references.
for (int i = 0, count = classMap.ReferenceMaps.Count; i < count; i++)
if (classMap.ReferenceMaps[i].Data.Member.MemberType() == typeof(TReferenceClass))
classMap.UpdateReference<TReferenceMap, TReferenceClass>(i, constructorArgs);
return classMap;
}
public static MemberReferenceMap UpdateReference<TReferenceMap, TReferenceClass>(this ClassMap classMap, int index, params object[] constructorArgs) where TReferenceMap : ClassMap<TReferenceClass>
{
if (index < 0 || index >= classMap.ReferenceMaps.Count)
throw new ArgumentException(nameof(index));
var oldRef = classMap.ReferenceMaps[index];
classMap.ReferenceMaps.RemoveAt(index);
var newRef = classMap.References(typeof(TReferenceMap), oldRef.Data.Member, constructorArgs);
if (oldRef.Data.Prefix != null)
newRef.Prefix(oldRef.Data.Prefix);
classMap.ReferenceMaps.Swap(index, classMap.ReferenceMaps.IndexOfWithHint(newRef, classMap.ReferenceMaps.Count - 1));
return newRef;
}
static Type? MemberType(this MemberInfo m) =>
m switch
{
PropertyInfo p => p.PropertyType,
FieldInfo f => f.FieldType,
_ => null,
};
static int IndexOfWithHint<T>(this IList<T> list, T item, int hint) where T : class
{
if (hint >= 0 && hint < list.Count && list[hint] == item)
return hint;
return list.IndexOf(item);
}
static void Swap<T>(this IList<T> list, int i, int j)
{
if (i != j)
{
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
And now serialize your BehaviouralData
as follows:
var config = new CsvConfiguration(CultureInfo.InvariantCulture);
using (var csv = new CsvWriter(writer, config))
{
csv.Context.RegisterClassMap<CustomAutoMap<BehaviouralData>>();
csv.WriteRecords(data);
}
Which results in:
ActionZ,ActionX,TargetBallAgentHashCode,CollectBehaviouralData,ActionTime,Time,x,y,z
0,0,1,False,01/01/0001 00:00:00,01/01/0001 00:00:00,1.1,1.1,1.1
0,0,2,False,01/01/0001 00:00:00,01/01/0001 00:00:00,2.1,2.1,2.1
0,0,3,False,01/01/0001 00:00:00,01/01/0001 00:00:00,3.1,3.1,3.1
If you would like the x
, y
and z
fields to have some prefix, add [HeaderPrefix("prefix")]
to TargetBallLocalPosition
like so:
public class BehaviouralData
{
[CsvHelper.Configuration.Attributes.HeaderPrefix("TargetBallLocation_")]
public Vector3 TargetBallLocalPosition { get; set; }
// Remainder unchanged
And you will get instead:
ActionZ,ActionX,TargetBallAgentHashCode,CollectBehaviouralData,ActionTime,Time,TargetBallLocation_x,TargetBallLocation_y,TargetBallLocation_z
0,0,1,False,01/01/0001 00:00:00,01/01/0001 00:00:00,1.1,1.1,1.1
0,0,2,False,01/01/0001 00:00:00,01/01/0001 00:00:00,2.1,2.1,2.1
0,0,3,False,01/01/0001 00:00:00,01/01/0001 00:00:00,3.1,3.1,3.1
If you need more manual control over the formatting of BehaviouralData
you will need to create a manual class map. When you do, use the method ClassMap<TClass>.References<TClassMap>(Expression<Func<TClass, object>> expression, params object[])
rather than ClassMap<TClass>.Map()
to reference the contents of Vector3
using your Vector3Map
:
public class BehaviouralDataMapExplicit : ClassMap<BehaviouralData>
{
public BehaviouralDataMapExplicit()
{
Map(m => m.ActionZ);
Map(m => m.ActionX);
Map(m => m.TargetBallAgentHashCode);
References<Vector3Map>(m => m.TargetBallLocalPosition)
.Prefix("TargetBallLocation_");
Map(m => m.CollectBehaviouralData);
Map(m => m.ActionTime);
Map(m => m.Time);
}
}
Demo fiddle here.
<sup>[1]</sup> CsvHelper seems to have a bug here. Specifically, for classes that do not implement IEnumerable
it attempts to automap indexed properties, resulting in the exception you see. This is a straightforward bug in CsvHelper, it should check that PropertyInfo.GetIndexParameters().Length == 0
before automapping any property discovered by reflection. But even if CsvHelper fixed this bug, you still wouldn't get what you want, which is the three fields of Vector3
, not its properties.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论