2011-01-23 9 views
1

コーダー、私はVS2010用のアドインを開発しており、コードエディタで選択したテキストを取得しようとしています。今のところ、私は多くのWebページを検索しており、あなたのすべてが使用しているようですDTE.ActiveDocumentこれは私のコードにエラーを引き起こします。私はエディタで選択されたテキストを返すと仮定するメソッドの2つのバージョンを書いたが、私は何度も何度も同じエラーが発生する:非静的なフィールド、メソッド、または財産法の「EnvDTE._DTE.ActiveDocument.get」ここ としている私の二つのバージョン(のみ関連するコードがあるshowen):VS 2010 addin:エディタで選択したテキストを取得

using EnvDTE; 

    private string getSelectedText_V1() 
    { 
     string selectedText = string.Empty; 

     /*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/ 
     Document doc = DTE.ActiveDocument; 

     return selectedText; 
    } 

    private string getSelectedText_V2() 
    { 
     string selectedText = string.Empty; 

     /*PROBLEM HERE: An object reference is required for the non-static field, method, or property 'EnvDTE._DTE.ActiveDocument.get'*/ 
     EnvDTE.TextSelection TxtSelection = DTE.ActiveDocument.Selection; 


     return selectedText; 
    } 

は、私は私のコードで間違っていたかを把握助けてください?

答えて

2

あなたのアドインでのgetService()メソッドへのアクセス権を持っている場合は、追加することができます:DTEとして

DTEのDTE = this.GetService(typeof演算(DTE))を、

次に、あなたのコードはなる:

private string getSelectedText_V1() 
{ 
    string selectedText = string.Empty; 
    DTE dte = this.GetService(typeof(DTE) as DTE; 

    Document doc = dte.ActiveDocument; 

    return doc.Selection.Text; 
} 
+0

もhttp://channel9.msdn.com/blogs/vsipmarketing/vsx101-an-introduction-to-visual-studio-2010-extensibility – ealshabaan

+0

作品見ますありがとう! PS:小さな構文エラーがあります。「as」の前に括弧がありません。それはあまりにも小さな変更なので、私はそれを編集することはできません。 –

関連する問題