Thursday, 15 July 2010

c# - Reading method values at runtime using reflection (+hack)? -



c# - Reading method values at runtime using reflection (+hack)? -

brief:

we have c# method take parameters.

inside of method need add together sql parameters (according method parameters).

it looks : http://i.stack.imgur.com/qp1y2.jpg (to enlarge)

please notice

i can write class have method arguments. , have create class container each sp ( , have lot).

ok lets continue.

i thinking myself : "why should insert each param manually - if have : name , type , value in method param ? don't want , maybe reflection me" ( lets leave conversion int <->dbtype.int32)

but encountered problem. not possible reading values using reflection.

however , did managed read values via reflection hack.

1) did example: ( simulation real scenario)

public static void mymethod(int addressid, string countryid, datetime dt) //these arguments need attached later { methodbase mi = methodinfo.getcurrentmethod(); var hack = new {addressid, countryid, dt}; //this hack var lstarguments = mi.getparameters() //get parameter name , types .select(p => new { name = p.name , type=p.gettype().name }).tolist(); list < dynamic > lstparamvalues= new list < dynamic > (); foreach(propertyinfo pi in hack.gettype().getproperties()) //get value(!!!) of each param { lstparamvalues.add(pi.getvalue(hack, null)); } dynamic dyn= mydbinstance; // dynamic reference db class instance ( for(int = 0; < lstarguments .count; i++) //dynamically invoke method param name+value. { dyn.addinparameter(lstarguments [i].name, ((lstparamvalues[i] int) ? dbtype.int32 : dbtype.string), lstparamvalues[i]); } }

and there db class ( simulate addinparameter see if im getting values)

class mydb { public void addinparameter(string name, dbtype dbt, object val) { console.writeline("name=" + name + " " + "dbtype=" + dbt + " " + "object val=" + val); } }

and working :

i executed method :

mymethod(1, "2", datetime.now);

and output :

name=addressid dbtype=int32 object val=1 name=countryid dbtype=string object val=2 name=dt dbtype=string object val=05/02/2013 19:09:32

all fine.

the c# question

how can on hack :

var hack = new {addressid, countryid, dt}; // method arguments names

in sample wrote hard coded.

the reflection method getvalue working because of line.

is there can add together on run-time method arguments name property , able :

foreach(propertyinfo pi in something.gettype().getproperties()) { }

please consider question c# question , not orm question.

also , if want play running sample code ( paste in console proj) : http://jsbin.com/azicut/1/edit

addwithvalue @ to the lowest degree save writing type name.

command.parameters.addwithvalue("@demographics", demoxml);

but shouln't pass much parameter function. simple dto class can create code much more readable. if not mistaken looking pyhton's locals (need locals in python) gives local variables of function key-value pair , can't in c# far know.

or simple not pass variables can pass dictionary<string,object> , add together key values sqlparameter

c# .net reflection

No comments:

Post a Comment