C#反射方法擴充

來源:互聯網
上載者:User

標籤:extension   property   dict   方法   task   type   交流   程式   key   

反射是程式猿的好幫手,有了反射你可以少寫一半的代碼。下面是一些常用的反射擴充方法。

 

using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace LiLi.Util{    public static class ReflectionExtension    {        public static IEnumerable<string> Keys(this Type type, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)        {            List<string> result = new List<string>();            result.AddRange(PropertyKeys(type, propBindingAttr));            result.AddRange(FieldKeys(type, fieldBindingAttr));            return result;        }        public static IEnumerable<string> PropertyKeys(this Type type, BindingFlags? bindingAttr = null)        {            PropertyInfo[] props = bindingAttr.HasValue ? type.GetProperties(bindingAttr.Value) : type.GetProperties();            return props.Select(x => x.Name);        }        public static IEnumerable<string> FieldKeys(this Type type, BindingFlags? bindingAttr = null)        {            FieldInfo[] fields = bindingAttr.HasValue ? type.GetFields(bindingAttr.Value) : type.GetFields();            return fields.Select(x => x.Name);        }        public static IDictionary<string, object> KeyValueList(this Type type, object obj, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)        {            Dictionary<string, object> result = new Dictionary<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)));            return result;        }        public static IDictionary<string, object> PropertyKeyValueList(this Type type, object obj, BindingFlags? bindingAttr = null)        {            Dictionary<string, object> result = new Dictionary<string, object>();            PropertyInfo[] props = bindingAttr.HasValue ? type.GetProperties(bindingAttr.Value) : type.GetProperties();            Array.ForEach(props, x => result.Add(x.Name, x.GetValue(obj)));            return result;        }        public static IDictionary<string, object> FieldKeyValueList(this Type type, object obj, BindingFlags? bindingAttr = null)        {            Dictionary<string, object> result = new Dictionary<string, object>();            FieldInfo[] fields = bindingAttr.HasValue ? type.GetFields(bindingAttr.Value) : type.GetFields();            Array.ForEach(fields, x => result.Add(x.Name, x.GetValue(obj)));            return result;        }        public static bool HasKey(this Type type, string key, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)        {            return type.Keys(propBindingAttr, fieldBindingAttr).Contains(key);        }        public static object GetValue(this Type type, string key, object obj, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)        {            IDictionary<string, object> propertyKeyValueList = PropertyKeyValueList(type, obj, propBindingAttr);            if (propertyKeyValueList.ContainsKey(key))            {                return propertyKeyValueList[key];            }            IDictionary<string, object> fieldKeyValueList = FieldKeyValueList(type, obj, fieldBindingAttr);            if (fieldKeyValueList.ContainsKey(key))            {                return fieldKeyValueList[key];            }            return null;        }    }}

 

如有高見,歡迎交流與分享:)

C#反射方法擴充

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.