簡體   English   中英

使用Roslyn的方法的參數驗證

[英]Parameter validation for a method using Roslyn

我想在編譯時為擴展方法執行參數驗證。

像這樣

在此處輸入圖片說明

這是我要驗證的程序的示例代碼

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
{

}

我打算使用Roslyn,但我不知道該Action要注冊並獲取通過的參數值。

使用Roslyn驗證方法參數的示例代碼將非常有幫助

您可以使用https://sharplab.io/查看代碼的語法樹。

您可能想注冊一個操作而不是一個操作:

context.RegisterOperationAction(YourAnalyzer, OperationKind.Invocation);

您可以在這些存儲庫中找到許多示例:

  1. https://github.com/dotnet/roslyn-analyzers
  2. https://github.com/DotNetAnalyzers/StyleCopAnalyzers
  3. https://github.com/code-cracker/code-cracker

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM