C # Reflection Method extension

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.