dynamic - in c#, how to call a function according to db value without if/else -
i trying call function according db value without using if/else or case. sample code below , think explain better.
logoobj.data ms = logoapp.newobj(logoobj.dataobjtype.item); // item
the "item" information comes select query. "item" "customer" well. therefore, dont want write code below.
if(datatable_result.rows[0][0].tostring()=="item") ms = logoapp.newobj(logoobj.dataobjtype.item); else if(datatable_result.rows[0][0].tostring()=="customer") ms = logoapp.newobj(logoobj.dataobjtype.customer); ......... ......... .........
can have piece of below, because need
logoobj.data ms = logoapp.newobj(logoobj.dataobjtype.xxxxxdb_valuexxxxx);
you can use enum.parse
:
var enumvalue = enum.parse(typeof(logoobj.dataobjtype), datatable_result.rows[0][0].tostring(), true); //true = ignore case ms = logoapp.newobj(enumvalue);
if have potential items in database not match of enum values, should instead use enum.tryparse
, check ensure has correctly parsed value before calling newobj
.
Comments
Post a Comment