Наследование: ICommandClassOrMethod
Пример #1
0
        public static Result <CommandMethod, Diagnostic> Create(
            CommandAttribute attribute,
            CommandClass commandClass,
            IMethodSymbol methodSymbol)
        {
            if (methodSymbol.TypeParameters.Length > 0)
            {
                return(Result.Err <CommandMethod, Diagnostic>(Diagnostic.Create(
                                                                  DiagnosticDescriptors.TypeParametersNotSupportedForCommands,
                                                                  commandClass.AttributeSyntax.GetLocation())));
            }

            if (methodSymbol.GetAttributes()
                .Any(attrData => SymbolEqualityComparer.Default.Equals(attrData.AttributeClass, commandClass.CommandManager.CommonSymbols.Tsu_CLI_Commands_RawInputAttribute)) &&
                (methodSymbol.Parameters.Length != 1 ||
                 methodSymbol.Parameters[0].Type.SpecialType != SpecialType.System_String))
            {
                return(Result.Err <CommandMethod, Diagnostic>(Diagnostic.Create(
                                                                  DiagnosticDescriptors.RawInputCommandMustHaveSingleStringParameter,
                                                                  commandClass.AttributeSyntax.GetLocation(),
                                                                  methodSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))));
            }

            var parameters = methodSymbol.Parameters;

            for (var index = 0; index < parameters.Length; index++)
            {
                var parameterSymbol = methodSymbol.Parameters[index];

                if (parameterSymbol.IsParams)
                {
                    if (index < methodSymbol.Parameters.Length - 1)
                    {
                        return(Result.Err <CommandMethod, Diagnostic>(Diagnostic.Create(
                                                                          DiagnosticDescriptors.ParamsMustBeLastArgument,
                                                                          commandClass.AttributeSyntax.GetLocation(),
                                                                          parameterSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
                                                                          methodSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))));
                    }
                    else if (parameterSymbol.Type is not IArrayTypeSymbol arrayTypeSymbol ||
                             arrayTypeSymbol.Rank != 1 ||
                             !Utilities.CanConvertToFromString((INamedTypeSymbol)arrayTypeSymbol.ElementType))
                    {
                        return(Result.Err <CommandMethod, Diagnostic>(Diagnostic.Create(
                                                                          DiagnosticDescriptors.ParamsMustBeSingleRankArrayOfConvertibleType,
                                                                          commandClass.AttributeSyntax.GetLocation(),
                                                                          parameterSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
                                                                          methodSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))));
                    }
                }
                else if (parameterSymbol.Type is not INamedTypeSymbol namedParameterType ||
                         !Utilities.CanConvertToFromString(namedParameterType))
                {
                    return(Result.Err <CommandMethod, Diagnostic>(Diagnostic.Create(
                                                                      DiagnosticDescriptors.NonConvertibleArgumentInCommandMethod,
                                                                      commandClass.AttributeSyntax.GetLocation(),
                                                                      parameterSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
                                                                      methodSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))));
                }
            }

            return(Result.Ok <CommandMethod, Diagnostic>(new CommandMethod(attribute, commandClass, methodSymbol)));
        }
Пример #2
0
 /// <summary>
 /// Initializes a new generated command
 /// </summary>
 /// <param name="name"></param>
 /// <param name="type"></param>
 /// <param name="methodSymbol"></param>
 private CommandMethod(CommandAttribute attribute, CommandClass commandClass, IMethodSymbol methodSymbol)
 {
     Attribute    = attribute ?? throw new ArgumentNullException(nameof(attribute));
     CommandClass = commandClass ?? throw new ArgumentNullException(nameof(commandClass));
     MethodSymbol = methodSymbol ?? throw new ArgumentNullException(nameof(methodSymbol));
 }