void ProcessLenInvocation(MethodInvocationExpression node)
{
if ((node.Arguments.Count < 1) || (node.Arguments.Count > 2))
{
Error(node, CompilerErrorFactory.MethodArgumentCount(node.Target, "len", node.Arguments.Count));
return;
}
Expression resultingNode = null;
Expression target = node.Arguments[0];
IType type = GetExpressionType(target);
bool isArray = IsAssignableFrom(TypeSystemServices.ArrayType, type);
if ((!isArray) && (node.Arguments.Count != 1))
{
Error(node, CompilerErrorFactory.MethodArgumentCount(node.Target, "len", node.Arguments.Count));
}
if (TypeSystemServices.IsSystemObject(type))
{
resultingNode = CodeBuilder.CreateMethodInvocation(MethodCache.RuntimeServices_Len, target);
}
else if (TypeSystemServices.StringType == type)
{
resultingNode = CodeBuilder.CreateMethodInvocation(target, MethodCache.String_get_Length);
}
else if (isArray)
{
if (node.Arguments.Count == 1)
resultingNode = CodeBuilder.CreateMethodInvocation(target, MethodCache.Array_get_Length);
else
resultingNode = CodeBuilder.CreateMethodInvocation(target, MethodCache.Array_GetLength, node.Arguments[1]);
}
else if (IsAssignableFrom(TypeSystemServices.ICollectionType, type))
{
resultingNode = CodeBuilder.CreateMethodInvocation(target, MethodCache.ICollection_get_Count);
}
else if (GenericsServices.HasConstructedType(type, TypeSystemServices.ICollectionGenericType))
{
resultingNode = new MemberReferenceExpression(node.LexicalInfo, target, "Count");
Visit(resultingNode);
}
else
{
Error(CompilerErrorFactory.InvalidLen(target, type));
}
if (null != resultingNode)
{
node.ParentNode.Replace(node, resultingNode);
}
}