c# - property names as params with compile check -


i'm writing utility class, take class name , array of property names params this:

public static void domagic(type type, params string[] include) 

and call looks this:

helper.domagic(typeof(myclass), "clientid", "pointid", "serialnumber") 

but don't it, couse there no compile-check, , if make mistake in string params, runtime error, not compile error.

i want this:

helper.domagic<myclass>(x => x.clientid, x => x.pointid, x => x.serialnumber) 

or, may be, shorter. there way this?

if want syntax

helper.domagic<myclass>(x => x.clientid, x => x.pointid, x => x.serialnumber) 

you need declare method

public static ienumerable<fieldinfo> domagic<t>(params expression<func<t, object>>[] include) {     foreach(expression<func<t, object>> tree in include)     {         fieldinfo fi = null;          // tree parser, gets field info          yield return fi;     } } 

so: retrieving property name lambda expression

it save typos, create other issues:

// method call domagic<myclass>(c => c.tostring().length); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -