c# - How to not specify the data type of arguments? -
i want adapter
function. parent class want datatable
, while derived class may pass in anything. i'm setting data type object
, cast it. not think pretty.
class parent { protected void add(datatable dt) { ... } // 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 javascript
, python
. it's common such things in "weak type" languages, , within can pass anywhere.
i've used <t>
, delegate
in c#
before. cannot think of way using them here.
what's c#
way of doing this?
you can use dynamic
keyword, interface implementation or abstract base class, prefer using typed members possible.
use of dynamic
:
public override void addraw(dynamic anything) { // pseudo code datatable dt = new datatable(); foreach(row r in anything) { dt.addrow(r); } this.add(dt); }
you can this, if want match , act based on type:
public override void addraw(object anything) { if (o mytable) { ... } else if (o string) { ... } }
Comments
Post a Comment