We had a custom object extension method that would handle the following.
- Source is
DataRow
target isclass
. - Source is
DataTable
target is aList<class>
- Source is
class
target isclass
- Source is
List<class>
target isList<class>
I found ValueInjecter and DataTable so I can handle a DataRow and DataTable.
So I am to the step where I am gluing it all together.
Here is what I tried.
public static class ObjectExtensions
{
public static void OldFill(this object fillMe, object sourceObject)
{
Type sourceType = sourceObject.GetType();
Type fillType = fillMe.GetType();
switch (sourceType.Name)
{
case "DataRow":
fillMe.InjectFrom<DataRowInjection>(sourceObject);
break;
case "DataTable":
fillMe.InjectFrom<DataTableInjection<fillType>>(sourceObject);
break;
default:
fillMe.InjectFrom(sourceObject);
break;
}
}
}
Not sure how to get the right fillType
to make the code work right.
Because this is legacy code I do not want to change the extension signature.
I don't know the answer, but I can say
DataTableInjection<fillType>
won't compile. You'd need to use Reflection to do the binding, something like this: