C#反射方法扩展

2021-05-05 15:26

阅读:494

标签: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 IEnumerablestring> Keys(this Type type, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)
        {
            Liststring> result = new Liststring>();
            result.AddRange(PropertyKeys(type, propBindingAttr));
            result.AddRange(FieldKeys(type, fieldBindingAttr));
            return result;
        }

        public static IEnumerablestring> 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 IEnumerablestring> 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 IDictionarystring, object> KeyValueList(this Type type, object obj, BindingFlags? propBindingAttr = null, BindingFlags? fieldBindingAttr = null)
        {
            Dictionarystring, object> result = new Dictionarystring, 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 IDictionarystring, object> PropertyKeyValueList(this Type type, object obj, BindingFlags? bindingAttr = null)
        {
            Dictionarystring, object> result = new Dictionarystring, 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 IDictionarystring, object> FieldKeyValueList(this Type type, object obj, BindingFlags? bindingAttr = null)
        {
            Dictionarystring, object> result = new Dictionarystring, 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)
        {
            IDictionarystring, object> propertyKeyValueList = PropertyKeyValueList(type, obj, propBindingAttr);
            if (propertyKeyValueList.ContainsKey(key))
            {
                return propertyKeyValueList[key];
            }
            IDictionarystring, object> fieldKeyValueList = FieldKeyValueList(type, obj, fieldBindingAttr);
            if (fieldKeyValueList.ContainsKey(key))
            {
                return fieldKeyValueList[key];
            }
            return null;
        }
    }
}

 

如有高见,欢迎交流与分享:)

C#反射方法扩展

标签:extension   property   dict   方法   task   type   交流   程序   key   

原文地址:http://www.cnblogs.com/yzbubble/p/7682915.html


评论


亲,登录后才可以留言!