System.Linq.Expressions.Expression.PropertyOrField C# (CSharp) Method

PropertyOrField() public static method

Creates a MemberExpression accessing a property or field.
public static PropertyOrField ( Expression expression, string propertyOrFieldName ) : MemberExpression
expression Expression The containing object of the member. This can be null for static members.
propertyOrFieldName string The member to be accessed.
return MemberExpression
        public static MemberExpression PropertyOrField(Expression expression, string propertyOrFieldName)
        {
            RequiresCanRead(expression, nameof(expression));
            // bind to public names first
            PropertyInfo pi = expression.Type.GetProperty(propertyOrFieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
            if (pi != null)
                return Property(expression, pi);
            FieldInfo fi = expression.Type.GetField(propertyOrFieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
            if (fi != null)
                return Field(expression, fi);
            pi = expression.Type.GetProperty(propertyOrFieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
            if (pi != null)
                return Property(expression, pi);
            fi = expression.Type.GetField(propertyOrFieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
            if (fi != null)
                return Field(expression, fi);

            throw Error.NotAMemberOfType(propertyOrFieldName, expression.Type, nameof(propertyOrFieldName));
        }

Usage Example

Ejemplo n.º 1
0
        public static Expression SelectPropertyValue(Type type, string property, List <string> values, QueryFilterExpressionCollection queryFilter)
        {
            var valueType             = GetPropertyType(type, property);
            var parsedValuesAsObjects = values.Select(v => TryConvert(v, valueType)).ToList();

            var parsedValues = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(valueType));

            foreach (var parsedValueAsObject in parsedValuesAsObjects)
            {
                parsedValues.Add(parsedValueAsObject);
            }

            var        param = Expression.Parameter(type, "i");
            Expression propertyExpression = param;

            foreach (var member in property.Split('.'))
            {
                propertyExpression = Expression.PropertyOrField(propertyExpression, member);
            }

            var expression = queryFilter.GetQueryFilterExpression(GetPropertyInfo(type, property));

            return(typeof(Lambda)
                   .GetMethod(nameof(Convert), BindingFlags.Static | BindingFlags.NonPublic)
                   .MakeGenericMethod(valueType, type)
                   .Invoke(null, new object[] { expression, parsedValues, propertyExpression, param })
                   as Expression);
        }
All Usage Examples Of System.Linq.Expressions.Expression::PropertyOrField
Expression