现在的位置: 首页 > 综合 > 正文

C#反射取值 源代码

2012年10月06日 ⁄ 综合 ⁄ 共 3132字 ⁄ 字号 评论关闭

        /// <summary>
        /// 获取对象中指定name的值 贾世义
        /// </summary>
        /// <param name="obj">DataRowView和实体类对象</param>
        /// <param name="name">字段或类成员</param>
        /// <returns></returns>
        public static object GetValue(object obj, string name)
        {
            if (obj == null || String.IsNullOrEmpty(name))
            {
                return null;
            }
            //DataRow优先
            if (obj is DataRowView || obj is DataRow)
            {
                return DataHelper.GetValue(obj, name);
            }
            //键值集合
            if (obj is NameValueCollection)
            {
                return ((NameValueCollection)obj)[name];
            }
            //实现了IDictionary接口的类
            if (obj.GetType().GetInterface("IDictionary", true) != null)
            {
                return ((IDictionary)obj)[name];
            }
            //类反射
            int p = name.IndexOf(".");
            if (p == -1)
            {
                int ps = name.IndexOf("(");
                if (ps == -1)
                {
                    //属性
                    PropertyInfo pInfo = obj.GetType().GetProperty(name);
                    if (pInfo != null)
                    {
                        return pInfo.GetValue(obj, null);
                    }
                    //字段
                    FieldInfo fInfo = obj.GetType().GetField(name);
                    if (fInfo != null)
                    {
                        return fInfo.GetValue(obj);
                    }
                    //方法
                    MethodInfo mInfo = obj.GetType().GetMethod(name);
                    if (mInfo != null)
                    {
                        return mInfo.Invoke(obj, null);
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    //带参数方法
                    int pe = name.IndexOf(")");
                    if (pe == -1)
                    {
                        pe = name.Length;
                    }
                    MethodInfo mInfo = obj.GetType().GetMethod(name.Substring(0, ps));
                    if (mInfo != null)
                    {
                        return mInfo.Invoke(obj, DataHelper.GetStrings(name.Substring(ps + 1, pe - ps - 1).Replace("'", "")));
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            else
            {
                //包含子类
                string name1 = name.Substring(0, p);
                object obj1 = null;
                PropertyInfo pInfo = obj.GetType().GetProperty(name1);
                if (pInfo != null)
                {
                    obj1 = pInfo.GetValue(obj, null);
                }
                else
                {
                    FieldInfo fInfo = obj.GetType().GetField(name1);
                    if (fInfo != null)
                    {
                        obj1 = fInfo.GetValue(obj);
                    }
                }
                if (obj1 == null)
                {
                    return null;
                }
                else
                {
                    return GetValue(obj1, name.Substring(p + 1));
                }
            }
        }

欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台

抱歉!评论已关闭.