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

GetProperty() private static method

private static GetProperty ( MethodInfo mi, string paramName, int index = -1 ) : PropertyInfo
mi System.Reflection.MethodInfo
paramName string
index int
return System.Reflection.PropertyInfo
        private static PropertyInfo GetProperty(MethodInfo mi, string paramName, int index = -1)
        {
            Type type = mi.DeclaringType;
            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic;
            flags |= (mi.IsStatic) ? BindingFlags.Static : BindingFlags.Instance;
            PropertyInfo[] props = type.GetProperties(flags);
            foreach (PropertyInfo pi in props)
            {
                if (pi.CanRead && CheckMethod(mi, pi.GetGetMethod(nonPublic: true)))
                {
                    return pi;
                }
                if (pi.CanWrite && CheckMethod(mi, pi.GetSetMethod(nonPublic: true)))
                {
                    return pi;
                }
            }
            throw Error.MethodNotPropertyAccessor(mi.DeclaringType, mi.Name, paramName, index);
        }

Usage Example

Ejemplo n.º 1
0
 /// <summary>
 /// Creates a <see cref="T:System.Linq.Expressions.MemberExpression"/> that represents accessing a property by using a property accessor method.
 /// </summary>
 ///
 /// <returns>
 /// A <see cref="T:System.Linq.Expressions.MemberExpression"/> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType"/> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.MemberAccess"/>, the <see cref="P:System.Linq.Expressions.MemberExpression.Expression"/> property set to <paramref name="expression"/> and the <see cref="P:System.Linq.Expressions.MemberExpression.Member"/> property set to the <see cref="T:System.Reflection.PropertyInfo"/> that represents the property accessed in <paramref name="propertyAccessor"/>.
 /// </returns>
 /// <param name="expression">An <see cref="T:System.Linq.Expressions.Expression"/> to set the <see cref="P:System.Linq.Expressions.MemberExpression.Expression"/> property equal to. This can be null for static properties.</param><param name="propertyAccessor">The <see cref="T:System.Reflection.MethodInfo"/> that represents a property accessor method.</param><exception cref="T:System.ArgumentNullException"><paramref name="propertyAccessor"/> is null.-or-The method that <paramref name="propertyAccessor"/> represents is not static (Shared in Visual Basic) and <paramref name="expression"/> is null.</exception><exception cref="T:System.ArgumentException"><paramref name="expression"/>.Type is not assignable to the declaring type of the method represented by <paramref name="propertyAccessor"/>.-or-The method that <paramref name="propertyAccessor"/> represents is not a property accessor method.</exception>
 public static MemberExpression Property(Expression expression, MethodInfo propertyAccessor)
 {
     return(Expression.Property(expression, Expression.GetProperty(propertyAccessor)));
 }
Expression