通过反射获得变量的值 - GetFieldValue

2017-02-26 15:46:13  访问(1332) 赞(0) 踩(0)


        /// <summary>
        /// 通过反射获得变量的值 - GetFieldValue
        /// </summary>
        /// <param name="oThis"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static object GetFieldValue(object oThis,string name)
        {
            if (oThis == null)
            {

                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "传入参数:object oThis为null。"
                    );
            }

            Type tThis = oThis.GetType();

            FieldInfo fieldInfoValue = tThis.GetField(name);

            if (fieldInfoValue == null)
            {

                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:FieldInfo fieldInfoValue = tThis.GetField(name["+name+"])值为null。"
                    );
            }

            return fieldInfoValue.GetValue(oThis);
        }


标签:通过反射获得变量的值 - GetFieldValue 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)