/// <summary> /// Creates a <see cref="MemberExpression" /> accessing a property. /// </summary> /// <param name="expression">The containing object of the property. This can be null for static properties.</param> /// <param name="property">The property to be accessed.</param> /// <returns>The created <see cref="MemberExpression" />.</returns> public static MemberExpression Property(Expression?expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, nameof(property)); var mi = property.GetGetMethod(true); if (mi == null) { mi = property.GetSetMethod(true); if (mi == null) { throw new ArgumentException($"The property '{property}' has no 'get' or 'set' accessors", nameof(property)); } if (mi.GetParameters().Length != 1) { throw new ArgumentException($"Incorrect number of arguments supplied for call to method '{mi}'", nameof(property)); } } else if (mi.GetParameters().Length != 0) { throw new ArgumentException($"Incorrect number of arguments supplied for call to method '{mi}'", nameof(property)); } if (mi.IsStatic) { if (expression != null) { throw new ArgumentException("Static property requires null instance, non-static property requires non-null instance.", nameof(expression)); } } else { if (expression == null) { throw new ArgumentException("Static property requires null instance, non-static property requires non-null instance.", nameof(property)); } ExpressionUtils.RequiresCanRead(expression, nameof(expression)); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw new ArgumentException($"Property '{property}' is not defined for type '{expression.Type}'", nameof(property)); } } ValidateMethodInfo(mi, nameof(property)); return(MemberExpression.Make(expression, property)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, nameof(property)); var mi = property.GetGetMethod(nonPublic: true); if (mi == null) { mi = property.GetSetMethod(nonPublic: true); if (mi == null) { throw Error.PropertyDoesNotHaveAccessor(property, nameof(property)); } if (mi.GetParameters().Length != 1) { throw Error.IncorrectNumberOfMethodCallArguments(mi, nameof(property)); } } else if (mi.GetParameters().Length != 0) { throw Error.IncorrectNumberOfMethodCallArguments(mi, nameof(property)); } if (mi.IsStatic) { if (expression != null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(expression)); } } else { if (expression == null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(property)); } ExpressionUtils.RequiresCanRead(expression, nameof(expression)); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw Error.PropertyNotDefinedForType(property, expression.Type, nameof(property)); } } ValidateMethodInfo(mi, nameof(property)); return(MemberExpression.Make(expression, property)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, "property"); MethodInfo mi = property.GetGetMethod(true); if (mi == null) { mi = property.GetSetMethod(true); if (mi == null) { throw Error.PropertyDoesNotHaveAccessor(property); } else if (mi.GetParametersCached().Length != 1) { throw Error.IncorrectNumberOfMethodCallArguments(mi); } } else if (mi.GetParametersCached().Length != 0) { throw Error.IncorrectNumberOfMethodCallArguments(mi); } if (mi.IsStatic) { if (expression != null) { throw new ArgumentException(Strings.OnlyStaticPropertiesHaveNullInstance, "expression"); } } else { if (expression == null) { throw new ArgumentException(Strings.OnlyStaticPropertiesHaveNullInstance, "property"); } RequiresCanRead(expression, "expression"); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw Error.PropertyNotDefinedForType(property, expression.Type); } } return(MemberExpression.Make(expression, property)); }
public static MemberExpression Field(Expression expression, FieldInfo field) { ContractUtils.RequiresNotNull(field, "field"); if (field.IsStatic) { ContractUtils.Requires(expression == null, "expression", Strings.OnlyStaticFieldsHaveNullInstance); } else { ContractUtils.Requires(expression != null, "field", Strings.OnlyStaticFieldsHaveNullInstance); RequiresCanRead(expression, "expression"); if (!TypeUtils.AreReferenceAssignable(field.DeclaringType, expression.Type)) { throw Error.FieldInfoNotDefinedForType(field.DeclaringType, field.Name, expression.Type); } } return(MemberExpression.Make(expression, field)); }
/// <summary> /// Creates a <see cref="T:System.Linq.Expressions.MemberExpression"/> that represents accessing a field. /// </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"/> and the <see cref="P:System.Linq.Expressions.MemberExpression.Expression"/> and <see cref="P:System.Linq.Expressions.MemberExpression.Member"/> properties set to the specified values. /// </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. For static (Shared in Visual Basic), <paramref name="expression"/> must be null.</param><param name="field">The <see cref="T:System.Reflection.FieldInfo"/> to set the <see cref="P:System.Linq.Expressions.MemberExpression.Member"/> property equal to.</param><exception cref="T:System.ArgumentNullException"><paramref name="field"/> is null.-or-The field represented by <paramref name="field"/> 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 field represented by <paramref name="field"/>.</exception> public static MemberExpression Field(Expression expression, FieldInfo field) { return(MemberExpression.Make(expression, field)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { return(MemberExpression.Make(expression, property)); }