c# - Lost in generic classes and inheritance - any anyone lead me out? -


i hope not complicated. have working, wonder if best can be:

i want use sql bulk copy class, , more specific overload takes idatareader. have data in several list<> need "translator". found 1 using extension methods - elegant, think! listed here: http://www.differentpla.net/content/2011/01/converting-ienumerablet-idatareader think excellent base build on?!

the code shown there not complete, unnecessary overloads never called missing. added them notimplementedexception. completed version can found @ end.

you use class calling asdatareader method on ienumerable<> or list<>, , pass number of columns in data, , function step on columns, this:

list<nodedb> mylist = new list<nodedb>(); mylist.add(mynode1); mybulk.writetoserver(mylist.asdatareader(3, nodedb.getvalue)); 

the class nodedb has fields, , static(!?) method getvalue gets iterated position (!?) , column, , returns appropriate item:

static object getvalue(nodedb nodedb, int i) {     object obj = null;     switch (i) {          case 0:             obj = nodedb.id;             break;         case 1:             obj = nodedb.latitude;              break;         case 2:              obj = nodedb.longitude;              break;     }     return obj; } 

having static method in class takes instance of sounds strange, wonder if there better way. more important: have several classes nodedb, of them specific getvalues, organized list<>, 1 list<> each type.

i pass down of them 1 generic bulk write method.

currently, passing different lists ienumberable, , cast after comparing them 1 one:

if (list list<nodedb>) { } else if ( .... 

within each of these different cases need address correct function too, repetive , duplicate , tedious - there way derive correct func argument list passed?

how change classes (to derive common class has abstract getvalues)? list<> given list then, think, , calls getvalues reach it's targets. tried that, static method cannot abstract in base class.

or can add parameter, type call func against that? or change idatareader extension?

as said, lost... help! ralf

here complete idatareader extension ienumerable interface implementations:

using system; using system.collections.generic; using system.data;  static class datareaderextensions {     public static idatareader asdatareader<tsource>(this ienumerable<tsource> source, int fieldcount, func<tsource, int, object> getvalue) {         return new enumerabledatareader<tsource>(source.getenumerator(), fieldcount, getvalue);         //return enumerabledatareader.create(source, fieldcount, getvalue);     } }  //internal static class enumerabledatareader { //    public static idatareader create<tsource>(ienumerable<tsource> source, int fieldcount, func<tsource, int, object> getvalue) { //        return new enumerabledatareader<tsource>(source.getenumerator(), fieldcount, getvalue); //    } //}  internal class enumerabledatareader<tsource> : idatareader {     private readonly ienumerator<tsource> _source;     private readonly int _fieldcount;     private readonly func<tsource, int, object> _getvalue;      internal enumerabledatareader(ienumerator<tsource> source, int fieldcount, func<tsource, int, object> getvalue) {         _source = source;         _getvalue = getvalue;         _fieldcount = fieldcount;     }      public void dispose() {         // nothing.     }      public string getname(int i) {         throw new notimplementedexception();     }      public string getdatatypename(int i) {         throw new notimplementedexception();     }      public type getfieldtype(int i) {         throw new notimplementedexception();     }      public object getvalue(int i) {         return _getvalue(_source.current, i);     }      public int getvalues(object[] values) {         throw new notimplementedexception();     }      public int getordinal(string name) {         throw new notimplementedexception();     }      public int fieldcount {         { return _fieldcount; }     }      object idatarecord.this[int i] {         { throw new notimplementedexception(); }     }      object idatarecord.this[string name] {         { throw new notimplementedexception(); }     }      public void close() {         throw new notimplementedexception();     }      public datatable getschematable() {         throw new notimplementedexception();     }      public bool nextresult() {         throw new notimplementedexception();     }      public bool read() {         return _source.movenext();     }      public int depth { get; private set; }     public bool isclosed { get; private set; }     public int recordsaffected { get; private set; }      public bool isdbnull(int i) {         throw new notimplementedexception();     }      public idatareader getdata(int i) {         throw new notimplementedexception();     }      public datetime getdatetime(int i) {         throw new notimplementedexception();     }      public decimal getdecimal(int i) {         throw new notimplementedexception();     }      public string getstring(int i) {         throw new notimplementedexception();     }      public double getdouble(int i) {         throw new notimplementedexception();     }      public float getfloat(int i) {         throw new notimplementedexception();     }      public int64 getint64(int i) {         throw new notimplementedexception();     }      public int32 getint32(int i) {         throw new notimplementedexception();     }      public int16 getint16(int i) {         throw new notimplementedexception();     }      public long getchars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) {         throw new notimplementedexception();     }      public guid getguid(int i) {         throw new notimplementedexception();     }      public long getbytes(int i, long fieldoffset, byte[] buffer, int bufferoffset, int length) {         throw new notimplementedexception();     }      public char getchar(int i) {         throw new notimplementedexception();     }      public byte getbyte(int i) {         throw new notimplementedexception();     }      public boolean getboolean(int i) {         throw new notimplementedexception();     }  } 

one possible solution make getvalue non-static , pass this:

list<nodedb> mylist = new list<nodedb>(); mylist.add(mynode1); mybulk.writetoserver(mylist.asdatareader(3, (n,i)=>n.getvalue(i))); 

the getvalue method this:

public object getvalue(int i)  {     object obj = null;     switch (i)      {         case 0:             obj = this.id;             break;         case 1:             obj = this.latitude;             break;         case 2:             obj = this.longitude;             break;     }     return obj; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -