The method
FillDerrivedFromBase(object classDerrived, object classBase)
copies all values of a base class to the derrived class. This works also fine with List<derrivedclass> and I don't have to worry about the types.
Simple way to fill the derrived instance with the data of the base instance:
public static class BaseDerrivedCopy
{
public static void DoTest()
{
var e0 = new LogBase.EntryBase();
e0.Title = "test";
System.Threading.Thread.Sleep(200);
var e1 = new LogImport.EntryImport();
e1.Filename = "file1.txt";
FillDerrivedFromBase(e1, e0);
}
public static void FillDerrivedFromBase(object classDerrived, object classBase)
{
foreach (var prodSrc in classBase.GetType().GetProperties())
{
classDerrived.GetType().GetProperty(prodSrc.Name).SetValue(classDerrived, prodSrc.GetValue(classBase));
}
}
public class EntryBase
{
public DateTime Timestamp { get; set; } = DateTime.Now;
public string Title { get; set; } = "";
}
public class EntryImport : EntryBase
{
public string Filename { get; set; }
}
}