2013-08-21 10 views
-2

獲得所選類別的類型這是我的代碼:如何從_applicationObject

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) 
     { 
      _applicationObject = (DTE2)application; 
      _addInInstance = (AddIn)addInInst; 
      if (connectMode == ext_ConnectMode.ext_cm_UISetup) 
      { 
       object[] contextGUIDS = new object[] { }; 
       Commands2 commands = (Commands2)_applicationObject.Commands; 

       CommandBar SECommandBar = ((CommandBars)_applicationObject.CommandBars)["Context Menus"]; 
       CommandBarPopup SEPopUps = (CommandBarPopup)SECommandBar.Controls["Project and Solution Context Menus"]; 
       CommandBarPopup ooCommandBar = (CommandBarPopup)SEPopUps.Controls["Project"]; 
       CommandBarPopup oCommandBar = (CommandBarPopup)SEPopUps.Controls["Item"]; 



       CommandBarControl oControl = (CommandBarControl) 
        oCommandBar.Controls.Add(MsoControlType.msoControlButton, 
        System.Reflection.Missing.Value, 
        System.Reflection.Missing.Value, 1, true); 
       // Set the caption of the menuitem 
       oControl.Caption = "Create Documentation"; 

       oSubMenuItemHandler = _applicationObject.Events.get_CommandBarEvents(oControl) as CommandBarEventsClass; 
       oSubMenuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(oSubMenuItemHandler_Click); 


      } 
     } 

public void oSubMenuItemHandler_Click(object CommandaBarControl, ref bool handled, ref bool cancelDefault) 
     { 
SelectedItems doc = _applicationObject.SelectedItems; 
// i want to get type of selected Class 
} 

我可以做這樣的事。 任何一個可以幫助我進入所選類的類型進行反思,並得到所有的方法和屬性

+2

一個'.cs'文件可以包含零至inifinite班級數量。請解釋你想要做什麼,而不是你認爲你的解決方案是什麼。 – CodeCaster

+0

讀取文件,其本質上是一個文本文件。只記得你的文件不會在一個已發佈的項目中(很可能),我不太清楚爲什麼你要公開cs文件,如果你要包含它的話。 – Sayse

+0

該文件與其中的類型無關。 – Lloyd

回答

3

CS 文件名沒有任何關係的類型存在於它。

這不是Java其中編譯器假裝每個文件都有一個類,而類命名爲文件本身(如果我沒有弄錯的話)。

所以,不,你不能這樣做C#

如果你需要閱讀C#編碼的文件,你可以有一個Roslyn做出適合您:

  • 讀取文件
  • 構建AST
  • 對您的回報類型的文件中。

可以看看這裏Read a .cs file, add property to class, write back to .cs file的具體例子。

+0

我開發了AddIn vs 2010,當右鍵單擊類並選擇我的項目並在項目的實現中單擊時,System._ComObject返回給我但我無法投射它到選定的類 –

+0

我修改了上面的代碼 –