C#反射 获取设置字段、属性值以及方法调用

using System.Reflection;

    public class Dynamic {
        /// <summary>
        /// 设置字段值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public static void SetFieldValue(object obj , string fieldName , object value) {
            FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            info.SetValue(obj , value);
        }
        /// <summary>
        /// 设置字段值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public static void SetFieldValue<T>(object obj , string fieldName , T value) {
            FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            info.SetValue(obj , value);
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue(object obj , string fieldName , object value) {
            PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            info.SetValue(obj , value);
        }
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue<T>(object obj , string fieldName , T value) {
            PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            info.SetValue(obj , value);
        }
        /// <summary>
        /// 字段取值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static object GetFieldValue(object obj , string fieldName) {
            FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            return info.GetValue(obj);
        }
        /// <summary>
        /// 字段取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static T GetFieldValue<T>(object obj , string fieldName) {
            FieldInfo info = obj.GetType().GetField(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            return (T)info.GetValue(obj);
        }
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static object GetPropertyValue(object obj , string fieldName) {
            PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            return info.GetValue(obj);
        }
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static T GetPropertyValue<T>(object obj , string fieldName) {
            PropertyInfo info = obj.GetType().GetProperty(fieldName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            return (T)info.GetValue(obj);
        }
        /// <summary>
        /// 获取方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="methodName"></param>
        /// <returns></returns>
        public static MethodInfo GetMethodInfo(object obj , string methodName) {
            MethodInfo info = obj.GetType().GetMethod(methodName , BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            return info;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值