【问题标题】:Add [ApiController] Attribute to the class在类中添加 [ApiController] 属性
【发布时间】:2021-03-08 09:23:26
【问题描述】:

我已经实现了将 Asp.Net WebApi 迁移到 Asp.Net Core 3.1 的项目。我已经开始学习 Rosyln 解析器。使用 Rosyln,我必须将“ApiController”属性更改为类名中的属性。

Sample.cs

namespace TestFile.TestData.Target
{   
  public class SampleFile: ApiController
  {        
  }
}

进入

namespace TestFile.TestData.Target
{   
  [ApiController]
  public class SampleFile: ControllerBase
  {        
  }
}

我已点击链接: Adding custom attributes to C# classes using Roslyn。但没看懂。

请就如何使用 Roslyn 的替代解决方案提出建议。

【问题讨论】:

  • 哪部分没看懂?
  • 不知道我们如何以及在哪里调用 addattribute 方法,而且 SyntaxTree 不包含 ParseCompilationUnit 的定义

标签: c# asp.net-core-webapi roslyn asp.net-core-3.1


【解决方案1】:

我终于明白了,

Sample.cs

private void AddCustomClassAttribute(string TargetClassFile, string CustomAttributeName)
{    
    var code = File.ReadAllText(TargetClassFile);
    var updateClassFile = InsertClassAttribute(code, CustomAttributeName).GetAwaiter().GetResult();
    if (!string.IsNullOrEmpty(updateClassFile))
        File.WriteAllText(TargetClassFile, updateClassFile);           
}

private async Task<string> InsertClassAttribute(string Code, string CustomAttributeName)
{
    // Parse the code into a SyntaxTree.
    var tree = CSharpSyntaxTree.ParseText(Code);
    // Get the root CompilationUnitSyntax.
    var root = await tree.GetRootAsync().ConfigureAwait(false) as CompilationUnitSyntax;    
    var findNamespace = root.Members.Single(m => m is NamespaceDeclarationSyntax) as NamespaceDeclarationSyntax;
    // Get all class declarations inside the namespace.
    var classDeclarations = findNamespace.Members.Where(m => m is ClassDeclarationSyntax);
    // find the main class from the findNameSapce
    var findRootClass = classDeclarations.First() as ClassDeclarationSyntax;
    var addCustomAttribute = AttributeList(
                                SingletonSeparatedList(
                                    Attribute(IdentifierName(CustomAttributeName)))
                                ).NormalizeWhitespace();

    // To check whether specific attribute is present in the class or not then only insert given attribute
    if (findRootClass.BaseList?.Types.ToFullString().Trim() == CustomAttributeName)
    {
        var attributes = findRootClass.AttributeLists.Add(addCustomAttribute);
        root = root.ReplaceNode(
            findRootClass,
            findRootClass.WithAttributeLists(attributes)).NormalizeWhitespace();
        return root.ToFullString();
    }
    return null;            
}

【讨论】:

    【解决方案2】:

    你想达到什么目的?您已成功添加[ApiController] 没有更多的额外步骤。如果您只是想添加不需要源生成器的属性,该链接是关于通过源生成器(编写代码的代码)添加属性的。

    顺便说一句,Roslyn 是 c# 编译器的名称。 Wich 用于创建整个应用程序,而不是用于向类添加属性的工具 :)

    如果您尝试通过源生成器生成类,可以稍微编辑一下问题

    【讨论】:

    • 好的,那么如何在类中添加自定义属性
    • 属性是一个类。要创建从 Attribute 继承的自定义属性创建类,还要在其名称中使用后缀“Attribute”(例如 public class MyNewCustomAttribute : Attribute)。应用与上面相同,只是用它装饰类。唯一奇怪的想法是未使用属性后缀,因此然后像这样添加属性 [MyNewCustom]
    猜你喜欢
    • 2019-06-17
    • 1970-01-01
    • 2020-05-06
    • 2011-01-06
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2021-06-20
    相关资源
    最近更新 更多