Exemplo n.º 1
0
        public override ReflectionHighlightingBase Validate(ReflectedTypeResolveResult resolvedType, IInvocationExpression invocation)
        {
            var reference = invocation.InvokedExpression as IReferenceExpression;

            if (resolvedType.TypeElement == null)
            {
                return(null);
            }

            int genericArgumentsCount = GetGenericArgumentsCount(resolvedType.TypeElement);

            if (genericArgumentsCount == 0)
            {
                return(new IncorrectMakeGenericTypeHighlighting(reference,
                                                                string.Format("Type '{0}' is not a generic type.",
                                                                              resolvedType.Type.GetPresentableName(CSharpLanguage.Instance))));
            }

            if (resolvedType.Type.IsResolved && !resolvedType.Type.IsOpenType)
            {
                return(new IncorrectMakeGenericTypeHighlighting(reference,
                                                                string.Format("Type '{0}' is closed generic type.",
                                                                              resolvedType.Type.GetPresentableName(CSharpLanguage.Instance))));
            }

            int  typeArgumentCount        = invocation.Arguments.Count;
            bool isArgumentPassedAsParams = false;

            if (typeArgumentCount != 0)
            {
                var typeParameters = invocation.Arguments[0].Expression as IArrayCreationExpression;

                if (typeParameters != null && typeParameters.ArrayInitializer != null)
                {
                    if (typeParameters.ArrayInitializer.ElementInitializers.Count != genericArgumentsCount)
                    {
                        return(new IncorrectMakeGenericTypeHighlighting(invocation.Arguments[0],
                                                                        string.Format("Incorrect count of type parameters for type {0}.",
                                                                                      resolvedType.Type.GetPresentableName(CSharpLanguage.Instance))));
                    }
                }

                var argumentExpression = invocation.Arguments[0].Expression;
                if (argumentExpression != null && argumentExpression.Type().IsType())
                {
                    isArgumentPassedAsParams = true;
                }
            }
            else
            {
                isArgumentPassedAsParams = true;
            }

            if (isArgumentPassedAsParams && typeArgumentCount != genericArgumentsCount)
            {
                var offset        = invocation.LPar.GetTreeStartOffset();
                var treeTextRange = new TreeTextRange(offset, invocation.RPar.GetTreeEndOffset());
                return(new IncorrectMakeGenericTypeHighlighting(invocation,
                                                                string.Format("Incorrect count of type parameters for type {0}.",
                                                                              resolvedType.Type.GetPresentableName(CSharpLanguage.Instance)),
                                                                IsValidTreeTextRange(treeTextRange) ? invocation.GetContainingFile().GetDocumentRange(treeTextRange) : (DocumentRange?)null));
            }

            return(null);
        }