reflection - pass "this" class type as a parameter in c# -
i want create common method classes, --->
restmethods.clearallstaticvalues(this);
so here passing argument, class reference. how can catch in method definition, in processing class fields (using reflection), doing in same class. code below--->
var varlist = this.gettype().getfields(bindingflags.nonpublic | bindingflags.static).tolist(); varlist.where(x => x.fieldtype == typeof(int32)).tolist().foreach(x => x.setvalue(this, 0));
note: don't wanna use this--->
class { restmethods.clearallstaticvalues(this); }
& method definition--->
restmethods.clearallstaticvalues(a args);
because class specific.
you can pass type
:
public static void clearallstaticvalues(type t) { var varlist = t.getfields(bindingflags.nonpublic | bindingflags.static); varlist.where(x => x.fieldtype == typeof(int32)).tolist().foreach(x => x.setvalue(null, 0)); }
call this:
public class { public static void clear() { //static member restmethods.clearallstaticvalues(typeof(a)); } public void clearinstance() { //instance member restmethods.clearallstaticvalues(gettype()); } }
here demo: http://ideone.com/oyqh5x
Comments
Post a Comment