1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast {
#else
namespace System.Linq.Expressions {
#endif
/// <summary>
/// Represents indexing a property or array.
/// </summary>
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.IndexExpressionProxy))]
#endif
public sealed class IndexExpression : Expression, IArgumentProvider {
private readonly Expression _instance;
private readonly PropertyInfo _indexer;
private IList<Expression> _arguments;
internal IndexExpression(
Expression instance,
PropertyInfo indexer,
IList<Expression> arguments) {
if (indexer == null) {
Debug.Assert(instance != null && instance.Type.IsArray);
Debug.Assert(instance.Type.GetArrayRank() == arguments.Count);
}
_instance = instance;
_indexer = indexer;
_arguments = arguments;
}
/// <summary>
/// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
/// </summary>
/// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
public sealed override ExpressionType NodeType {
get { return ExpressionType.Index; }
}
/// <summary>
/// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
/// </summary>
/// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
public sealed override Type Type {
get {
if (_indexer != null) {
return _indexer.PropertyType;
}
return _instance.Type.GetElementType();
}
}
/// <summary>
/// An object to index.
/// </summary>
public Expression Object {
get { return _instance; }
}
/// <summary>
/// Gets the <see cref="PropertyInfo"/> for the property if the expression represents an indexed property, returns null otherwise.
/// </summary>
public PropertyInfo Indexer {
get { return _indexer; }
}
/// <summary>
/// Gets the arguments to be used to index the property or array.
/// </summary>
public ReadOnlyCollection<Expression> Arguments {
get { return ReturnReadOnly(ref _arguments); }
}
/// <summary>
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="object">The <see cref="Object" /> property of the result.</param>
/// <param name="arguments">The <see cref="Arguments" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public IndexExpression Update(Expression @object, IEnumerable<Expression> arguments) {
if (@object == Object && arguments == Arguments) {
return this;
}
return Expression.MakeIndex(@object, Indexer, arguments);
}
Expression IArgumentProvider.GetArgument(int index) {
return _arguments[index];
}
int IArgumentProvider.ArgumentCount {
get {
return _arguments.Count;
}
}
/// <summary>
/// Dispatches to the specific visit method for this node type.
/// </summary>
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitIndex(this);
}
internal Expression Rewrite(Expression instance, Expression[] arguments) {
Debug.Assert(instance != null);
Debug.Assert(arguments == null || arguments.Length == _arguments.Count);
return Expression.MakeIndex(instance, _indexer, arguments ?? _arguments);
}
}
public partial class Expression {
/// <summary>
/// Creates an <see cref="IndexExpression"/> that represents accessing an indexed property in an object.
/// </summary>
/// <param name="instance">The object to which the property belongs. Should be null if the property is static(shared).</param>
/// <param name="indexer">An <see cref="Expression"/> representing the property to index.</param>
/// <param name="arguments">An IEnumerable{Expression} contaning the arguments to be used to index the property.</param>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression MakeIndex(Expression instance, PropertyInfo indexer, IEnumerable<Expression> arguments) {
if (indexer != null) {
return Property(instance, indexer, arguments);
} else {
return ArrayAccess(instance, arguments);
}
}
#region ArrayAccess
/// <summary>
/// Creates an <see cref="IndexExpression"></see> to access an array.
/// </summary>
/// <param name="array">An expression representing the array to index.</param>
/// <param name="indexes">An array containing expressions used to index the array.</param>
/// <remarks>The expression representing the array can be obtained by using the MakeMemberAccess method,
/// or through NewArrayBounds or NewArrayInit.</remarks>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression ArrayAccess(Expression array, params Expression[] indexes) {
return ArrayAccess(array, (IEnumerable<Expression>)indexes);
}
/// <summary>
/// Creates an <see cref="IndexExpression"></see> to access an array.
/// </summary>
/// <param name="array">An expression representing the array to index.</param>
/// <param name="indexes">An <see cref="IEnumerable{Expression}"/> containing expressions used to index the array.</param>
/// <remarks>The expression representing the array can be obtained by using the MakeMemberAccess method,
/// or through NewArrayBounds or NewArrayInit.</remarks>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression ArrayAccess(Expression array, IEnumerable<Expression> indexes) {
RequiresCanRead(array, "array");
Type arrayType = array.Type;
if (!arrayType.IsArray) {
throw Error.ArgumentMustBeArray();
}
var indexList = indexes.ToReadOnly();
if (arrayType.GetArrayRank() != indexList.Count) {
throw Error.IncorrectNumberOfIndexes();
}
foreach (Expression e in indexList) {
RequiresCanRead(e, "indexes");
if (e.Type != typeof(int)) {
throw Error.ArgumentMustBeArrayIndexType();
}
}
return new IndexExpression(array, null, indexList);
}
#endregion
#region Property
/// <summary>
/// Creates an <see cref="IndexExpression"/> representing the access to an indexed property.
/// </summary>
/// <param name="instance">The object to which the property belongs. If the property is static/shared, it must be null.</param>
/// <param name="propertyName">The name of the indexer.</param>
/// <param name="arguments">An array of <see cref="Expression"/> objects that are used to index the property.</param>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression Property(Expression instance, string propertyName, params Expression[] arguments) {
RequiresCanRead(instance, "instance");
ContractUtils.RequiresNotNull(propertyName, "indexerName");
PropertyInfo pi = FindInstanceProperty(instance.Type, propertyName, arguments);
return Property(instance, pi, arguments);
}
#region methods for finding a PropertyInfo by its name
/// <summary>
/// The method finds the instance property with the specified name in a type. The property's type signature needs to be compatible with
/// the arguments if it is a indexer. If the arguments is null or empty, we get a normal property.
/// </summary>
private static PropertyInfo FindInstanceProperty(Type type, string propertyName, Expression[] arguments) {
// bind to public names first
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy;
PropertyInfo pi = FindProperty(type, propertyName, arguments, flags);
if (pi == null) {
flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy;
pi = FindProperty(type, propertyName, arguments, flags);
}
if (pi == null) {
if (arguments == null || arguments.Length == 0) {
throw Error.InstancePropertyWithoutParameterNotDefinedForType(propertyName, type);
} else {
throw Error.InstancePropertyWithSpecifiedParametersNotDefinedForType(propertyName, GetArgTypesString(arguments), type);
}
}
return pi;
}
private static string GetArgTypesString(Expression[] arguments) {
StringBuilder argTypesStr = new StringBuilder();
var isFirst = true;
argTypesStr.Append("(");
foreach (var t in arguments.Select(arg => arg.Type)) {
if (!isFirst) {
argTypesStr.Append(", ");
}
argTypesStr.Append(t.Name);
isFirst = false;
}
argTypesStr.Append(")");
return argTypesStr.ToString();
}
private static PropertyInfo FindProperty(Type type, string propertyName, Expression[] arguments, BindingFlags flags) {
MemberInfo[] members = type.FindMembers(MemberTypes.Property, flags, Type.FilterNameIgnoreCase, propertyName);
if (members == null || members.Length == 0)
return null;
PropertyInfo pi;
var propertyInfos = members.Map(t => (PropertyInfo)t);
int count = FindBestProperty(propertyInfos, arguments, out pi);
if (count == 0)
return null;
if (count > 1)
throw Error.PropertyWithMoreThanOneMatch(propertyName, type);
return pi;
}
private static int FindBestProperty(IEnumerable<PropertyInfo> properties, Expression[] args, out PropertyInfo property) {
int count = 0;
property = null;
foreach (PropertyInfo pi in properties) {
if (pi != null && IsCompatible(pi, args)) {
if (property == null) {
property = pi;
count = 1;
}
else {
count++;
}
}
}
return count;
}
private static bool IsCompatible(PropertyInfo pi, Expression[] args) {
MethodInfo mi;
mi = pi.GetGetMethod(true);
ParameterInfo[] parms;
if (mi != null) {
parms = mi.GetParametersCached();
} else {
mi = pi.GetSetMethod(true);
//The setter has an additional parameter for the value to set,
//need to remove the last type to match the arguments.
parms = mi.GetParametersCached().RemoveLast();
}
if (mi == null) {
return false;
}
if (args == null) {
return parms.Length == 0;
}
if (parms.Length != args.Length)
return false;
for (int i = 0; i < args.Length; i++) {
if (args[i] == null) return false;
if (!TypeUtils.AreReferenceAssignable(parms[i].ParameterType, args[i].Type)) {
return false;
}
}
return true;
}
#endregion
/// <summary>
/// Creates an <see cref="IndexExpression"/> representing the access to an indexed property.
/// </summary>
/// <param name="instance">The object to which the property belongs. If the property is static/shared, it must be null.</param>
/// <param name="indexer">The <see cref="PropertyInfo"/> that represents the property to index.</param>
/// <param name="arguments">An array of <see cref="Expression"/> objects that are used to index the property.</param>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression Property(Expression instance, PropertyInfo indexer, params Expression[] arguments) {
return Property(instance, indexer, (IEnumerable<Expression>)arguments);
}
/// <summary>
/// Creates an <see cref="IndexExpression"/> representing the access to an indexed property.
/// </summary>
/// <param name="instance">The object to which the property belongs. If the property is static/shared, it must be null.</param>
/// <param name="indexer">The <see cref="PropertyInfo"/> that represents the property to index.</param>
/// <param name="arguments">An <see cref="IEnumerable{T}"/> of <see cref="Expression"/> objects that are used to index the property.</param>
/// <returns>The created <see cref="IndexExpression"/>.</returns>
public static IndexExpression Property(Expression instance, PropertyInfo indexer, IEnumerable<Expression> arguments) {
var argList = arguments.ToReadOnly();
ValidateIndexedProperty(instance, indexer, ref argList);
return new IndexExpression(instance, indexer, argList);
}
// CTS places no restrictions on properties (see ECMA-335 8.11.3),
// so we validate that the property conforms to CLS rules here.
//
// Does reflection help us out at all? Expression.Property skips all of
// these checks, so either it needs more checks or we need less here.
private static void ValidateIndexedProperty(Expression instance, PropertyInfo property, ref ReadOnlyCollection<Expression> argList) {
// If both getter and setter specified, all their parameter types
// should match, with exception of the last setter parameter which
// should match the type returned by the get method.
// Accessor parameters cannot be ByRef.
ContractUtils.RequiresNotNull(property, "property");
if (property.PropertyType.IsByRef) throw Error.PropertyCannotHaveRefType();
if (property.PropertyType == typeof(void)) throw Error.PropertyTypeCannotBeVoid();
ParameterInfo[] getParameters = null;
MethodInfo getter = property.GetGetMethod(true);
if (getter != null) {
getParameters = getter.GetParametersCached();
ValidateAccessor(instance, getter, getParameters, ref argList);
}
MethodInfo setter = property.GetSetMethod(true);
if (setter != null) {
ParameterInfo[] setParameters = setter.GetParametersCached();
if (setParameters.Length == 0) throw Error.SetterHasNoParams();
// valueType is the type of the value passed to the setter (last parameter)
Type valueType = setParameters[setParameters.Length - 1].ParameterType;
if (valueType.IsByRef) throw Error.PropertyCannotHaveRefType();
if (setter.ReturnType != typeof(void)) throw Error.SetterMustBeVoid();
if (property.PropertyType != valueType) throw Error.PropertyTyepMustMatchSetter();
if (getter != null) {
if (getter.IsStatic ^ setter.IsStatic) throw Error.BothAccessorsMustBeStatic();
if (getParameters.Length != setParameters.Length - 1) throw Error.IndexesOfSetGetMustMatch();
for (int i = 0; i < getParameters.Length; i++) {
if (getParameters[i].ParameterType != setParameters[i].ParameterType) throw Error.IndexesOfSetGetMustMatch();
}
} else {
ValidateAccessor(instance, setter, setParameters.RemoveLast(), ref argList);
}
}
if (getter == null && setter == null) {
throw Error.PropertyDoesNotHaveAccessor(property);
}
}
private static void ValidateAccessor(Expression instance, MethodInfo method, ParameterInfo[] indexes, ref ReadOnlyCollection<Expression> arguments) {
ContractUtils.RequiresNotNull(arguments, "arguments");
ValidateMethodInfo(method);
if ((method.CallingConvention & CallingConventions.VarArgs) != 0) throw Error.AccessorsCannotHaveVarArgs();
if (method.IsStatic) {
if (instance != null) throw Error.OnlyStaticMethodsHaveNullInstance();
} else {
if (instance == null) throw Error.OnlyStaticMethodsHaveNullInstance();
RequiresCanRead(instance, "instance");
ValidateCallInstanceType(instance.Type, method);
}
ValidateAccessorArgumentTypes(method, indexes, ref arguments);
}
private static void ValidateAccessorArgumentTypes(MethodInfo method, ParameterInfo[] indexes, ref ReadOnlyCollection<Expression> arguments) {
if (indexes.Length > 0) {
if (indexes.Length != arguments.Count) {
throw Error.IncorrectNumberOfMethodCallArguments(method);
}
Expression[] newArgs = null;
for (int i = 0, n = indexes.Length; i < n; i++) {
Expression arg = arguments[i];
ParameterInfo pi = indexes[i];
RequiresCanRead(arg, "arguments");
Type pType = pi.ParameterType;
if (pType.IsByRef) throw Error.AccessorsCannotHaveByRefArgs();
TypeUtils.ValidateType(pType);
if (!TypeUtils.AreReferenceAssignable(pType, arg.Type)) {
if (!TryQuote(pType, ref arg)) {
throw Error.ExpressionTypeDoesNotMatchMethodParameter(arg.Type, pType, method);
}
}
if (newArgs == null && arg != arguments[i]) {
newArgs = new Expression[arguments.Count];
for (int j = 0; j < i; j++) {
newArgs[j] = arguments[j];
}
}
if (newArgs != null) {
newArgs[i] = arg;
}
}
if (newArgs != null) {
arguments = new TrueReadOnlyCollection<Expression>(newArgs);
}
} else if (arguments.Count > 0) {
throw Error.IncorrectNumberOfMethodCallArguments(method);
}
}
#endregion
}
}
|