2017-05-14 2 views
0

私はので、私は、私は(選択RoslynのInvocationExpressionSyntaxに基づいてMethodDeclarationSyntaxを取得する方法はありますか? InvocationExpressionSyntaxとして

class Program 
{ 
    [Obsolete()] 
    public static void A() { } 

    static void Main(string[] args) 
    { 
     A(); // I moved the mouse here and captured as InvocationExpressionSyntax 
     Console.WriteLine("Hello World!"); 
    } 
} 

のようなコードを書いたロスリンによってコードリファクタリングを書きたい)ので、私は知りたいのは、私がMethodDeclarationSyntaxまたは選択方法の優れた属性を取得することができます。

はそれが可能です

public static void A() { } 

または

[Obsolete()] 

を意味しますか?

リファクタリング目的のメソッドのすべての属性を検索したいと考えています。

EDIT

public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) 
{ 
    // TODO: Replace the following code with your own analysis, generating a CodeAction for each refactoring to offer 

    var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); 

    // Find the node at the selection. 
    var node = root.FindNode(context.Span); 

    // Only offer a refactoring if the selected node is a type declaration node. 
    var typeDecl = node.Parent as InvocationExpressionSyntax; 
    if (typeDecl == null) 
    { 
     return; 
    } 

    // For any type declaration node, create a code action to reverse the identifier text. 
    var action = CodeAction.Create("Reverse type name", c => ReverseTypeNameAsync(context.Document, typeDecl, c)); 

    // Register this code action. 
    context.RegisterRefactoring(action); 
} 

EDIT 2

ClassLibrary1の:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)] 
    public class CustomAttribute : Attribute 
    {} 

ClassLibrary2

public static class Class1 
{ 
    [CustomAttribute] // Comes from ClassLibrary1 
    public static string SayHelloTo(string name) 
    { 
     return $"Hello {name}"; 
    } 

コードのリファクタリングプロジェクト:

class Program 
{ 
    [Obsolete()] 
    public static void A() { } 

    static void Main(string[] args) 
    { 
     A(); // I moved the mouse here and captured as InvocationExpressionSyntax 
     var t = ClassLibrary2.Class1.SayHelloTo("Test"); // ????? Symbol is NULL ????? 
    } 
} 
+1

ここにあなたの状況は?基本的に 'SemanticModel'を取得し、' model.GetSymbolInfo(invocation) 'を呼びたいと思っています。それがシンボルを解決すると、シンボル上で 'GetAttributes()'を呼び出すことができます。しかし、あなたは意味モデルが必要です... –

+0

@JonSkeet、私は自分の投稿を編集しました。サンプルはコードリファクタリングVSテンプレートから来ました。 'var typeDecl = node.Parent from InvocationExpressionSyntax;'私はSemanticModelが必要だと言っています。どうすればいいですか?上記の情報源に基づいてもっと説明できますか? –

+0

よく 'Document'を与えられれば、あなたは' Document.GetSemanticModelAsync'を呼び出すことができます... –

答えて

0

私はあなただけで、セマンティックモデルを得るシンボルのためにそれを聞いて、および属性を取得したい疑う:

// After you've made sure that you've got an InvocationExpressionSyntax 
var model = await context.Document 
    .GetSemanticModelAsync(context.CancellationToken) 
    .ConfigureAwait(false); 
var symbol = model.GetSymbolInfo(invocation); 
var attributes = symbol.Symbol?.GetAttributes(); 
// Examine attributes; it will be null if the symbol wasn't resolved 

Iドン意味モデルを得ることがどれほど高価であるかを知っている - より効果的な選択肢があるかもしれないが、少なくともこれはうまくいくと思う。

+0

ありがとう、あなたのソリューションは上記のサンプルに対して完璧に動作しますが、私は別のアセンブリで定義された属性と別のアセンブリでプログラムされたメソッドを取得したいと思います。GetAttributes ()は現在のプロジェクトのそのメソッドからは動作していないようです。あなたが言ったようにそれはnullです。解決策はありますか?外部ソースコードでも動作しますか?私は再び投稿を編集しました –

+0

@ user7489391:スタックオーバーフローの仕組みではありません。あなたが求めているものを変更し続けることはありません。インタラクティブなデバッグセッションではありません。コンパイラがそれらを解決できる限り、 'GetAttributes()'が他のアセンブリからのメソッドや属性にうまく対処することを期待しています。 –

関連する問題