GQL018: Parser method must be valid

Value
Rule ID GQL018
Category Usage
Default severity Error
Enabled by default Yes
Code fix provided No
Introduced in v8.1

Cause

This rule is triggered when parser method specified by the Parser attribute has invalid signature.

Rule description

The method specified by the Parser attribute must be:

  • static
  • return object
  • have a single argument of type object

Additionally, if the specified method is defined in a different class from where the attribute is applied, the method must be declared as public.

How to fix violations

Fix the method signature to match the required pattern.

Example of a violation

public class TestClass
{
    [Parser(nameof(Parse))]
    public string Hello1 { get; set; }

    [Parser(typeof(Parsers), nameof(Parsers.ParseValue))]
    public string Hello2 { get; set; }

    // wrong argument type
    private static object Parse(string value) => Convert.ToInt32(value);
}

public class Parsers
{
    // must be public
    internal static object ParseValue(object value) => Convert.ToInt32(value);
}

Example of how to fix

Fix the attribute argument to match the method name

public class TestClass
{
    [Parser(nameof(Parse))]
    public string Hello1 { get; set; }

    [Parser(typeof(Parsers), nameof(Parsers.ParseValue))]
    public string Hello2 { get; set; }

    private static object Parse(object value) => Convert.ToInt32(value);
}

public class Parsers
{
    public static object ParseValue(object value) => Convert.ToInt32(value);
}

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable GQL018
// The code that's violating the rule is on this line.
#pragma warning restore GQL018

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.cs]
dotnet_diagnostic.GQL018.severity = none

For more information, see How to suppress code analysis warnings.

GQL017: Could not find method
GQL019: Validator method must be valid
GQL020: ValidateArguments method must be valid