Due to a recent issue i had some days ago (you can check this post here), i needed to create a way that some of my linq to sql tables could be referenced dynamically. I’ve managed to do that through an abstract LogTable class, which contains all my LogTable properties as defined on my DataTable. I was able to do this abstract way because my LogTables have the same structure, thus the same properties.
Heres the reducted code:
Abstract base class
public abstract class LogTableStructure
{
public int ID { get; set; }
public datetime? LastAccess { get; set; }
public string Username { get; set; }
}
My (reducted) dynamic method to update a LogTable:
public void UpdateLog<T>(T currentLOG) where T : LogTableStructure
{
LogTableStructure logStructure = null;
//LogTableEnum is defined on this class constructor
switch (LogTableEnum)
{
case LogTableEnum.Log2009:
logStructure = this.factory.LogDB.LOG_2009s
.SingleOrDefault(q => q.ID == currentLOG.ID);
break;
case LogTableEnum.Log2010:
logStructure = this.factory.LogDB.LOG_2010s
.SingleOrDefault(q => q.ID == currentLOG.ID);
break;
case LogTableEnum.Log2011:
logStructure = this.factory.LogDB.LOG_2011s
.SingleOrDefault(q => q.ID == currentLOG.ID);
break;
}
}
PROBLEM
for some reason the currentLOG param throws a runtime null reference exception, even though it has all LogTable properties filled. I’ve notice by using vs2010 debbuger that while the currentLOG properties are filled, the base class (LogTableStructure) properties are all empty, as if the base object is null.
Am i forgeting something about member hide inheritance or something alike? I’ve even added the new modifier to all my LogTable properties on my .dbml, but even that didn’t solve the problem
Just make the object into which you’re injecting an actual instance:
That should do it.