我正在解析CSV文件并将数据放在结构中.我使用的是TextFieldParser从这个问题和它的工作相似,但它返回一个魅力String[].目前我有一个丑陋的过程:
String[] row = parser.ReadFields();
DispatchCall call = new DispatchCall();
if (!int.TryParse(row[0], out call.AccountID)) {
Console.WriteLine("Invalid Row: " + parser.LineNumber);
continue;
}
call.WorkOrder = row[1];
call.Description = row[2];
call.Date = row[3];
call.RequestedDate = row[4];
call.EstStartDate = row[5];
call.CustomerID = row[6];
call.CustomerName = row[7];
call.Caller = row[8];
call.EquipmentID = row[9];
call.Item = row[10];
call.TerritoryDesc = row[11];
call.Technician = row[12];
call.BillCode = row[13];
call.CallType = row[14];
call.Priority = row[15];
call.Status = row[16];
call.Comment = row[17];
call.Street = row[18];
call.City = row[19];
call.State = row[20];
call.Zip = row[21];
call.EquipRemarks = row[22];
call.Contact = row[23];
call.ContactPhone = row[24];
call.Lat = row[25];
call.Lon = row[26];
call.FlagColor = row[27];
call.TextColor = row[28];
call.MarkerName = row[29];
Run Code Online (Sandbox Code Playgroud)
结构由所有那些字段组成,String除了AccountID是一个int.令我很生气的是,他们并没有强力打字,但现在就让我们看看.鉴于parser.ReadFields()返回a String[]是否有更有效的方法来填充结构(可能转换某些值,如row[0]需要成为一个int)与数组中的值?
**编辑:**我忘了提到的一个限制,可能影响什么样的解决方案将起作用,这个结构[Serializable]将被发送到其他地方的Tcp.
您的里程数可能因其是否是更好的解决方案而有所不同,但您可以使用反射并定义Attribute用于标记结构成员的类.该属性将数组索引作为参数.然后,通过使用反射来分配右数组元素的值.
您可以像这样定义属性:
[AttributeUsage(AttributeTargets.Property)]
public sealed class ArrayStructFieldAttribute : Attribute
{
public ArrayStructFieldAttribute(int index)
{
this.index = index;
}
private readonly int index;
public int Index {
get {
return index;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着该属性可以简单地用于将int名称Index与属性相关联.
然后,您可以使用该属性在结构中标记您的属性(只是一些示例性的行):
[ArrayStructField(1)]
public string WorkOrder { // ...
[ArrayStructField(19)]
public string City { // ...
Run Code Online (Sandbox Code Playgroud)
然后可以使用Type结构类型的对象设置值(您可以使用typeof运算符获取它):
foreach (PropertyInfo prop in structType.GetProperties()) {
ArrayStructFieldAttribute attr = prop.GetCustomAttributes(typeof(ArrayStructFieldAttribute), false).Cast<ArrayStructFieldAttribute>().FirstOrDefault();
if (attr != null) {
// we have found a property that you want to load from an array element!
if (prop.PropertyType == typeof(string)) {
// the property is a string property, no conversion required
prop.SetValue(boxedStruct, row[attr.Index]);
} else if (prop.PropertyType == typeof(int)) {
// the property is an int property, conversion required
int value;
if (!int.TryParse(row[attr.Index], out value)) {
Console.WriteLine("Invalid Row: " + parser.LineNumber);
} else {
prop.SetValue(boxedStruct, value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码迭代结构类型的所有属性.对于每个属性,它会检查上面定义的自定义属性类型.如果存在此类属性,并且属性类型为string或int,则从相应的数组索引复制该值.
我正在检查string和int属性,因为这是您在问题中提到的两种数据类型.即使您现在只有一个包含int值的特定索引,如果此代码准备将任何索引作为字符串或int属性处理,那么它的可维护性也很好.
请注意,类型更多数量的处理,我没有使用链建议if和else if,而是Dictionary<Type, Func<string, object>>该属性类型映射到转换功能.