【问题标题】:How to specify Roslyn method parameters如何指定 Roslyn 方法参数
【发布时间】:2018-05-25 19:03:11
【问题描述】:

我有以下代码,但我认为参数不太正确。我如何实际使用 Roslyn 代码生成位来指定参数?请注意,它也必须是array。谢谢!

using static SyntaxFactory;
using static SyntaxKind;
using static LanguageVersion;
// ...
MemberDeclarationSyntax GeneratePrivateBytesCtor()
    => ConstructorDeclaration(Descriptor.TypeIdentifier)
        .WithModifiers(SyntaxTokenList.Create(Token(PrivateKeyword)))
        .AddParameterListParameters(ParseParameterList("byte[] bytes").Parameters.Single())
        .WithInitializer(ConstructorInitializer(BaseConstructorInitializer)
            .AddArgumentListArguments(
                Argument(IdentifierName("bytes"))
            )
        );

【问题讨论】:

    标签: c# parameters constructor roslyn


    【解决方案1】:

    经过一番挖掘和一些修补,我想知道这是否是一个更好的方法?

    const string bytes = nameof(bytes);
    const SyntaxKind arrayType = SyntaxKind.ArrayType;
    
    var bytesSyntax = Parameter(Identifier(bytes))
        .WithModifiers(Create(Token(arrayType)))
        .WithType(ParseTypeName(typeof(byte).FullName))
        ;
    

    再次假设某些静态 using 语句。

    或者,甚至可能是这样的:

    var bytesSyntax = Parameter(Identifier(bytes))
        .WithType(ParseTypeName(typeof(byte[]).FullName))
        ;
    

    基于对 *Reflection** 的遥远经验的假设,其中我似乎记得“数组”被标识为类型“修饰符”,沿着这些思路。我可能弄错了;已经有一段时间了,但在这里我发现我想将参数声明为 byte[] 字面意思。

    想法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2013-01-26
      相关资源
      最近更新 更多