I have the following class hierachy:
public class DataDumpBase {
public string propA {get; set;}
public int propB {get; set;}
public DateTime? propC {get; set;}
//and so on up to 80 properties more
}
public class DataDumpLine : DataDumpBase {
public List<decimal> Values {get; set; }
}
I need to create a new DataDumpLine and initialize it with the values from a DataDumpBase object.
I tried add this method in DataDumpBase
public DataDumpLine CreateLine() {
DataDumpLine result = new DataDumpLine();
foreach (PropertyInfo pi in this.GetType().GetProperties()) {
pi.SetValue(result, pi.GetValue(this, null), null);
}
return result;
}
So I could do:
var line = dumpRows[i].CreateLine();
But the line pi.SetValue(result, pi.GetValue(this, null), null);
throws a System.Reflection.TargetException exception: Object does not match target type.
The code is very similar to C# Using Reflection to copy base class properties
I don't figure out what is wrong
Aucun commentaire:
Enregistrer un commentaire