private static void ValidateAccessor(Expression?instance, MethodInfo method, ParameterInfo[] indexes, ref ReadOnlyCollection <Expression> arguments, string?paramName) { ContractUtils.RequiresNotNull(arguments, nameof(arguments)); ValidateMethodInfo(method, nameof(method)); if ((method.CallingConvention & CallingConventions.VarArgs) != 0) { throw Error.AccessorsCannotHaveVarArgs(paramName); } if (method.IsStatic) { if (instance != null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(instance)); } } else { if (instance == null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(instance)); } ExpressionUtils.RequiresCanRead(instance, nameof(instance)); ValidateCallInstanceType(instance.Type, method); } ValidateAccessorArgumentTypes(method, indexes, ref arguments, paramName); }
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)); }