Exemplo n.º 1
0
        /// <summary>
        /// 获取属性或字段的值
        /// </summary>
        /// <param name="container">原对象</param>
        /// <param name="propName">属性或字段名,有参数属性为参数值</param>
        /// <returns></returns>
        public object CallPropertyOrField(object container, string propName)
        {
            Type t = container.GetType();

            //此处的属性包括有参属性(索引)与无参属性(属性)
            //if (propName.IndexOfAny(indexExprStartChars) < 0)
            //因属性与字段均不可能以数字开头,如第一个字符为数字则直接跳过属性判断以加快处理速度
            if (!char.IsDigit(propName[0]))
            {
#if !NET20_NOTUSER
                PropertyInfo p = DynamicHelpers.GetPropertyInfo(t, propName);
                //取属性
                if (p != null)
                {
                    return(p.GetValue(container, null));
                }
#if NEEDFIELD
                //取字段
                FieldInfo f = t.GetField(propName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | _bindingIgnoreCase);
                if (f != null)
                {
                    return(f.GetValue(container));
                }
#endif
#else
                System.Linq.Expressions.MemberExpression exp;
#if NEEDFIELD
                exp = System.Linq.Expressions.Expression.PropertyOrField(System.Linq.Expressions.Expression.Constant(container), propName);
#else
                exp = System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(container), propName);
#endif
                if (exp != null)
                {
                    return(System.Linq.Expressions.Expression.Lambda(exp).Compile().DynamicInvoke());
                }
#endif
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the property or field value of the object.
        /// </summary>
        /// <param name="container">The object.</param>
        /// <param name="type">The type of the object.</param>
        /// <param name="name">The property or field name. </param>
        /// <returns></returns>
        public static object CallPropertyOrField(object container, string name, Type type = null)
        {
            Type t = type ?? container.GetType();

            if (!char.IsDigit(name[0]))
            {
#if !NET20_NOTUSER
                PropertyInfo p = DynamicHelpers.GetPropertyInfo(t, name);
                //Property
                if (p != null)
                {
                    return(p.GetValue(container, null));
                }
#if NEEDFIELD
                //Field
                FieldInfo f = t.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase);
                if (f != null)
                {
                    return(f.GetValue(container));
                }
#endif
#else
                System.Linq.Expressions.MemberExpression exp;
#if NEEDFIELD
                exp = System.Linq.Expressions.Expression.PropertyOrField(System.Linq.Expressions.Expression.Constant(container), name);
#else
                exp = System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(container), name);
#endif
                if (exp != null)
                {
                    return(System.Linq.Expressions.Expression.Lambda(exp).Compile().DynamicInvoke());
                }
#endif
            }

            return(null);
        }