我有一个DAL,它可以访问多个CSV文件,不断更新格式,并不断添加新格式。
CSV文件中的每一行代表系统中的一个类,但由于格式很多,输入是通过两步处理来处理的。
步骤1.将CSV文件中的行转换为值列表。将此值列表转换为" raw"对象
然后,这个原始对象将被映射到Business逻辑对象,因此所有原始对象都将被转换为业务逻辑对象。
但是,由于某些csv行非常倾向于使用嵌套类生成类,因此我的通用映射器目前不太有用。
映射对象是一个属性,具有属性上由属性指定的值字段的索引位置。
public static T BuildRawObject(string[] values)
{
T target = (T) Activator.CreateInstance(typeof(T), new object[] { });
Type targetInfo = target.GetType();
foreach (PropertyInfo pi in targetInfo.GetProperties())
{
if (!pi.PropertyType.IsValueType)
{
if (pi.PropertyType.IsAssignableFrom(typeof(NestedObj1)))
{
var result = BuildRawObject2<NestedObj1>(values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else if(pi.PropertyType.IsAssignableFrom(typeof(NestedObj2)))
{
var result = BuildRawObject2<NestedObj2>(values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else if (pi.PropertyType.IsAssignableFrom(typeof(NestedObj3)))
{
var result = BuildRawObject2<NestedObj3>(values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else if (pi.PropertyType.IsAssignableFrom(typeof(NestedObj5)))
{
var result = BuildRawObject2<NestedObj5>(values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else if (pi.PropertyType.IsAssignableFrom(typeof(NestedObj6)))
{
var result = BuildRawObject2<NestedObj6>(values);
try
{
pi.SetValue(target, result);
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else if (pi.PropertyType.IsAssignableFrom(typeof(NestedObj7)))
{
var result = BuildRawObject2<NestedObj7>(values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else
{
Console.WriteLine("An unrecognized class exists in the raw object. This will not be mapped to: " + pi.Name);
}
}
else
{
Mapping mapping = pi.GetCustomAttribute<Mapping>();
if (mapping != null)
{
values[mapping.SourceIndex] = EmptyStringValuesWhenEmpty(values[mapping.SourceIndex]);
if (pi.PropertyType == typeof(DateTime) || pi.PropertyType == typeof(DateTime?))
{
bool valid = DateTime.TryParse(values[mapping.SourceIndex], out DateTime dateTime);
if (valid)
{
pi.SetValue(target, dateTime, null);
}
else
{
Console.WriteLine("Value: " + values[mapping.SourceIndex] + " on position[" +
mapping.SourceIndex + "] was not a valid date time. Value is required.");
}
}
else
{
try
{
pi.SetValue(target, GenericConverter(values[mapping.SourceIndex], pi.PropertyType), null);
}
catch (Exception e)
{
Console.WriteLine("Exception on index: " + mapping.SourceIndex + " contained value: " +
values[mapping.SourceIndex] + " was attempted for property type: " +
pi.PropertyType +
"Parse was not possible. Either input file is in wrong format, or RawInputObject has incorect property");
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
}
else
{
Console.WriteLine("Mapping to field: " + pi.Name +
" was skipped due to missing mapping attribute definition.");
}
}
}
return target;
}
当我尝试为&#34; Raw返回对象&#34;的嵌套类赋值时出现问题。
理想情况下,我希望将整个事物作为递归方法。 但是,父类与嵌套类的类型不同。 所以我必须生成一个新方法,它与父类相同但接受嵌套类
然而,在理论上,生成一个可能在泛型方法中出现的命名类型对象的无限列表只是......错误。 我宁愿简单地让程序从任何专门的嵌套类推断到方法,以确定方法的返回类型。 这可能吗?我无法弄清楚如何。
如果我不清楚我的问题,或者您对如何更好地提出问题有任何建议,请告诉我。如果缺乏细节。请指教,我将提供。
编辑:提供解决方案后的方法
public static object BuildRawObject(Type objectType, string[] values)
{
dynamic target = Activator.CreateInstance(objectType, new object[] { });
Type targetInfo = target.GetType();
foreach (PropertyInfo pi in targetInfo.GetProperties())
{
if (pi.PropertyType.GetInterfaces().AsEnumerable().Contains(typeof(IRawObjectBase)) || pi.PropertyType.GetInterfaces().AsEnumerable().Contains(typeof(IRawNestedObjectBase)))
{
var result = BuildRawObject(pi.PropertyType, values);
try
{
pi.SetValue(target, Convert.ChangeType(result, pi.PropertyType));
}
catch (Exception e)
{
Console.WriteLine("Exception on property: " + pi.PropertyType +
" was bit not to set built instance on property. Instace was: " + result);
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
else
{
Mapping mapping = pi.GetCustomAttribute<Mapping>();
if (mapping != null)
{
values[mapping.SourceIndex] = EmptyStringValuesWhenEmpty(values[mapping.SourceIndex]);
if (pi.PropertyType == typeof(DateTime) || pi.PropertyType == typeof(DateTime?))
{
bool valid = DateTime.TryParse(values[mapping.SourceIndex], out DateTime dateTime);
if (valid)
{
pi.SetValue(target, dateTime, null);
}
else
{
Console.WriteLine("Value: " + values[mapping.SourceIndex] + " on position[" +
mapping.SourceIndex + "] was not a valid date time. Value is required.");
}
}
else
{
try
{
pi.SetValue(target, GenericConverter(values[mapping.SourceIndex], pi.PropertyType), null);
}
catch (Exception e)
{
Console.WriteLine("Exception on index: " + mapping.SourceIndex + " contained value: " +
values[mapping.SourceIndex] + " was attempted for property type: " +
pi.PropertyType +
"Parse was not possible. Either input file is in wrong format, or RawInputObject has incorect property");
Console.WriteLine("Exception mesessage was: " + e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception message was: " + e.InnerException.Message);
}
}
}
}
else
{
Console.WriteLine("Mapping to field: " + pi.Name +
" was skipped due to missing mapping attribute definition.");
}
}
}
return target;
}
答案 0 :(得分:1)
在这种情况下,似乎不必要地使用泛型会让你感到厌烦。如果您将签名更改为
static object BuildRawObject(Type objectType, string[] values)
您可以直接在那里传递属性类型,而无需对属性类型进行大量检查:
if (propertyIsNotPrimitiveType) {
var result = BuildRawObject(pi.PropertyType, values);
}
为方便起见,您仍然可以保留通用超载:
static T BuildRawObject<T>(string[] values) {
return (T) BuildRawObject(typeof(T), values);
}