private Expression ParseElementAccess(Expression expr)
{
int errorPos = _token.Position;
var args = ParseBracketExpression(true);
if (expr.Type.IsArray)
{
if (expr.Type.GetArrayRank() != 1 || args.Length != 1)
throw ParseError(errorPos, Res.CannotIndexMultiDimArray);
Expression index = PromoteExpression(args[0], typeof(int), true);
if (index == null)
throw ParseError(errorPos, Res.InvalidIndex);
return Expression.ArrayIndex(expr, index);
}
MethodBase mb;
switch (FindIndexer(expr.Type, args, out mb))
{
case 0:
throw ParseError(errorPos, Res.NoApplicableIndexer(GetTypeName(expr.Type)));
case 1:
return Expression.Call(expr, (MethodInfo) mb, args);
default:
throw ParseError(errorPos, Res.AmbiguousIndexerInvocation(GetTypeName(expr.Type)));
}
}