Exemple #1
0
 static IEnumerable <IMethodSymbol> GetOnChangedMethods(INamedTypeSymbol classSymbol, string methodName, string parameterType) =>
 CommandHelper.GetMethods(classSymbol,
                          methodSymbol => methodSymbol.ReturnsVoid && methodSymbol.Name == methodName && methodSymbol.Parameters.Length < 2 &&
                          (methodSymbol.Parameters.Length == 0 || methodSymbol.Parameters.First().Type.ToDisplayStringNullable() == parameterType));
Exemple #2
0
        static string GetMethod(ContextInfo info, INamedTypeSymbol classSymbol, IFieldSymbol fieldSymbol, string methodName, string defaultMethodName, string parameterName, string parameterType)
        {
            var hasMethodName = methodName != null;

            if (!hasMethodName)
            {
                methodName = defaultMethodName;
            }

            var methods = GetOnChangedMethods(classSymbol, methodName, parameterType);

            if (methods.Count() == 2)
            {
                info.Context.ReportTwoSuitableMethods(classSymbol, fieldSymbol, methodName, parameterType);
                return(methodName + "(" + parameterName + ");");
            }
            if (methods.Count() == 1)
            {
                if (methods.First().Parameters.Length == 0)
                {
                    return(methodName + "();");
                }
                return(methodName + "(" + parameterName + ");");
            }

            if (!hasMethodName)
            {
                return(string.Empty);
            }
            info.Context.ReportOnChangedMethodNotFound(fieldSymbol, methodName, parameterType, CommandHelper.GetMethods(classSymbol, methodName));
            return(null);
        }
Exemple #3
0
        public static CommandGenerator Create(ContextInfo info, INamedTypeSymbol classSymbol, IMethodSymbol methodSymbol)
        {
            var hasError       = false;
            var isCommand      = methodSymbol.ReturnsVoid;
            var isAsyncCommand = methodSymbol.ReturnType.ToDisplayStringNullable().StartsWith("System.Threading.Tasks.Task");

            if (methodSymbol.Parameters.Length > 1 || !(isCommand || isAsyncCommand))
            {
                info.Context.ReportIncorrectCommandSignature(methodSymbol);
                hasError = true;
            }

            var parameterType        = methodSymbol.Parameters.FirstOrDefault()?.Type.ToDisplayStringNullable() ?? string.Empty;
            var canExecuteMethodName = CommandHelper.GetCanExecuteMethodName(methodSymbol, info.CommandAttributeSymbol);

            if (canExecuteMethodName == null)
            {
                var candidate = CommandHelper.GetCanExecuteMethodCandidates(classSymbol, "Can" + methodSymbol.Name, parameterType);
                canExecuteMethodName = candidate.SingleOrDefault()?.Name ?? "null";
            }
            else
            {
                var candidates = CommandHelper.GetCanExecuteMethodCandidates(classSymbol, canExecuteMethodName, parameterType);
                if (!candidates.Any())
                {
                    info.Context.ReportCanExecuteMethodNotFound(methodSymbol, canExecuteMethodName, parameterType, CommandHelper.GetMethods(classSymbol, canExecuteMethodName));
                    hasError = true;
                }
            }
            return(hasError ? null : new CommandGenerator(methodSymbol, info.CommandAttributeSymbol, parameterType, canExecuteMethodName, isCommand));
        }