I want an Adapter
like function. The parent class want DataTable
, while its derived class may pass in anything. I'm now setting the data type to object
and then cast it. But I do not think this is pretty.
class Parent
{
protected void Add(DataTable dt) { ... } // the real business logics
public virtual void AddRaw(object anything) {}
}
class Child1 : Parent
{
public override void AddRaw(object anything)
{
MyTable1 t = (MyTable1) anything;
// pseudo code
DataTable dt = new DataTable();
foreach(row r in t)
{
dt.AddRow(r);
}
this.Add(dt);
}
}
class Child2 : Parent
{
public override void AddRaw(object anything)
{
MyTable2 t = (MyTable2) anything;
...
}
}
I've background from javascript
and python
. It's so common to do such things in a "weak type" languages, and within which you can pass anything anywhere.
I've used <T>
and delegate
in C#
before. But I cannot think of a way using them here.
What's the C#
way of doing this?