簡體   English   中英

如何指定Roslyn方法參數

[英]How to specify Roslyn method parameters

我有以下代碼,但我認為該參數不正確。 我實際上如何使用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"))
            )
        );

經過一番挖掘和修補之后,我想知道這是否是更好的方法?

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[]

有什么想法嗎?

暫無
暫無

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

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