我想访问选定的类或接口。当右键点击项目,新项目的后面添加到VS 2010我想、右键点击、新项目、后面

2023-09-08 08:33:19 作者:看,最美的那个是我媳妇儿

我这样做code:

   `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 oCommandBar = (CommandBarPopup)SEPopUps.Controls["Project"];

                    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);
}

 protected void oSubMenuItemHandler_Click(object CommandaBarControl,ref bool handled, ref bool cancelDefault)
        {
// I Want to access object of selected class or interface
            MessageBox.Show("Test");                
        }`

我制定中添加,以反映类在VS 2010中的所有数据。 请我要访问选择的类或接口,以反映所有成员国的数据。 任何一个可以帮助我

I'm Develop Add In to reflect all data of class in vs 2010. Please I want to access selected class or Interface to Reflect all member data. any one help me

推荐答案

您可以得到有效的 项目 所描述的此处,然后得到它的 ProjectItems ,并为每个的 项目项 它的 文件codeModel 然后遍历其 codeElements ,提供的 = vsCMElementInterface 得到的接口定义有

You can get the active Project as described here, then get its ProjectItems and for each ProjectItem its FileCodeModel and then iterate over its CodeElements with Kind= vsCMElementInterface to get the Interfaces defined there

示例

// Container for results
List<string> classes = new List<string> ();
List<string> interfaces = new List<string> ();

// Get selected projects from solution explorer
Array projects = (Array)_applicationObject.ActiveSolutionProjects;

// Get all ProjectItems inside of the selected Projects
var projectItems = projects
    .OfType<Project> ()
    .Where ( p => p.ProjectItems != null )
    .SelectMany ( p => p.ProjectItems.OfType<ProjectItem> ().Where ( pi => pi.FileCodeModel != null ) );

// Iterate over all of these ProjectItems 
foreach ( ProjectItem projectItem in projectItems )
{
    // Get all of the CodeElements (Interfaces and Classes) inside of the current ProjectItem (recursively)
    var elements = projectItem.FileCodeModel.CodeElements
        .OfType<CodeElement> ()
        .SelectMany ( ce => this.GetCodeElements ( ce ) );

    // Do something with the CodeElements that were found
    classes.AddRange ( elements.Where ( el => el.Kind == vsCMElement.vsCMElementClass ).Select ( el => el.Name ) );
    interfaces.AddRange ( elements.Where ( el => el.Kind == vsCMElement.vsCMElementInterface).Select ( el => el.Name ) );
}


// Possible implementation of GetCodeElements:
private IEnumerable<CodeElement> GetCodeElements ( CodeElement root )
{
    List<CodeElement> result = new List<CodeElement> ();
    if ( root == null )
        return result;

    // If the current CodeElement is an Interface or a class, add it to the results
    if ( root.Kind == vsCMElement.vsCMElementClass || root.Kind == vsCMElement.vsCMElementInterface )
    {
        result.Add ( root );
    }

    // Check children recursively
    if ( root.Children != null && root.Children.Count > 0 )
    {
        foreach ( var item in root.Children.OfType<CodeElement> () )
        {
            result.AddRange ( this.GetCodeElements ( item ) );
        }
    }
}