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
Post a Comment