0

I want to perform parameter validation at compile time for an extension method.

Something like this

enter image description here

Here is the sample code of my program to validate

public class Program
{
    static void Main(string[] args)
    {
        var sample = new Sample();
        var output = sample.SampleMethod("To Validate");  // I want to validate this param
    }
}

public static class Ext
{
    public static string SampleMethod(this Sample sample, string sampleParam)
    {
        return sampleParam + " Hello";
    }
}

public class Sample
{

}

I am planning to use Roslyn but I don't know the Action to register and to get the parameter value that passed.

Sample code to validate parameter for a method using Roslyn will be very helpful

Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59

1 Answers1

1

You can use https://sharplab.io/ to look at the syntax tree of your code.

You might want to register an operation instead of an action:

context.RegisterOperationAction(YourAnalyzer, OperationKind.Invocation);

You can find lots of examples in these repos:

  1. https://github.com/dotnet/roslyn-analyzers
  2. https://github.com/DotNetAnalyzers/StyleCopAnalyzers
  3. https://github.com/code-cracker/code-cracker
Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59