Reflection is a good helper for a program ape, with a reflection you can write less than half of the code. Here are some common methods of reflection extension.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespacelili.util{ Public Static classreflectionextension { Public Staticienumerable<string> Keys ( ThisType type, BindingFlags? Propbindingattr =NULL, BindingFlags? Fieldbindingattr =NULL) {List<string> result =Newlist<string>(); Result. AddRange (PropertyKeys (Type, propbindingattr)); Result. AddRange (Fieldkeys (Type, fieldbindingattr)); returnresult; } Public Staticienumerable<string> PropertyKeys ( ThisType type, BindingFlags? bindingattr =NULL) {propertyinfo[] props= Bindingattr.hasvalue?type. GetProperties (Bindingattr.value): type. GetProperties (); returnProps. Select (x =x.name); } Public Staticienumerable<string> Fieldkeys ( ThisType type, BindingFlags? bindingattr =NULL) {fieldinfo[] fields= Bindingattr.hasvalue?type. GetFields (Bindingattr.value): type. GetFields (); returnFields. Select (x =x.name); } Public Staticidictionary<string,Object> Keyvaluelist ( ThisType type,Objectobj, BindingFlags? Propbindingattr =NULL, BindingFlags? Fieldbindingattr =NULL) {Dictionary<string,Object> result =Newdictionary<string,Object>(); propertyinfo[] Props= Propbindingattr.hasvalue?type. GetProperties (Propbindingattr.value): type. GetProperties (); Array.foreach (props, x=result. ADD (X.name, X.getvalue (obj))); Fieldinfo[] Fields= Fieldbindingattr.hasvalue?type. GetFields (Fieldbindingattr.value): type. GetFields (); Array.foreach (fields, x=result. ADD (X.name, X.getvalue (obj))); returnresult; } Public Staticidictionary<string,Object> Propertykeyvaluelist ( ThisType type,Objectobj, BindingFlags? bindingattr =NULL) {Dictionary<string,Object> result =Newdictionary<string,Object>(); propertyinfo[] Props= Bindingattr.hasvalue?type. GetProperties (Bindingattr.value): type. GetProperties (); Array.foreach (props, x=result. ADD (X.name, X.getvalue (obj))); returnresult; } Public Staticidictionary<string,Object> Fieldkeyvaluelist ( ThisType type,Objectobj, BindingFlags? bindingattr =NULL) {Dictionary<string,Object> result =Newdictionary<string,Object>(); Fieldinfo[] Fields= Bindingattr.hasvalue?type. GetFields (Bindingattr.value): type. GetFields (); Array.foreach (fields, x=result. ADD (X.name, X.getvalue (obj))); returnresult; } Public Static BOOLHaskey ( ThisType type,stringKey, BindingFlags? Propbindingattr =NULL, BindingFlags? Fieldbindingattr =NULL) { returntype. Keys (Propbindingattr, fieldbindingattr). Contains (key); } Public Static ObjectGetValue ( ThisType type,stringKeyObjectobj, BindingFlags? Propbindingattr =NULL, BindingFlags? Fieldbindingattr =NULL) {IDictionary<string,Object> propertykeyvaluelist =propertykeyvaluelist (type, obj, propbindingattr); if(Propertykeyvaluelist.containskey (key)) {returnPropertykeyvaluelist[key]; } IDictionary<string,Object> fieldkeyvaluelist =fieldkeyvaluelist (type, obj, fieldbindingattr); if(Fieldkeyvaluelist.containskey (key)) {returnFieldkeyvaluelist[key]; } return NULL; } }}
If you have any ideas, you are welcome to Exchange and share:)
C # Reflection Method extension