.net - BindingFlags for Type.GetMethods excluding property accessors -
suppose i've got following program:
namespace reflectiontest { public class example { private string field; public void methodone() { } public void methodtwo() { } public string property { { return field; } set { this.field = value; } } } class program { static void main(string[] args) { iterate(typeof(example)); console.readline(); } public static void iterate(type type) { methodinfo[] methods = type.getmethods( bindingflags.declaredonly | bindingflags.instance | bindingflags.public); foreach (methodinfo mi in methods) { console.writeline(mi.name); } } } }
when run program i'm getting following output:
methodone methodtwo get_property set_property
i want skip property accesor methods. i've tried different bindingflags
, instance, ~bindingflags.setproperty
, no luck. @ moment way i've found skip methods rewriting iterate function to:
public static void iterate(type type) { methodinfo[] methods = type.getmethods( bindingflags.declaredonly | bindingflags.instance | bindingflags.public); foreach (methodinfo mi in methods) { if (mi.isspecialname) continue; console.writeline(mi.name); } }
do know bindingflags
should use?
update:
well, should have explained project building automatically templates unit testing, can skip special methods. additional information on isspecialname :)
linq? really? wow. anyway, project .net 2.0 linq (sadly) not option.
from top of head:
mi.isspecialname &&( mi.name.startswith("set_") || mi.name.startswith("get_"))
should set. specialname more property accessors (event add/remove methods count here well), that's why have check names well.
you can use linq :)
Comments
Post a Comment