【问题标题】:I Want to access Selected Class or Interface When Right Click to project after add in new item to vs 2010 [closed]在将新项目添加到vs 2010后,我想在右键单击项目时访问选定的类或接口[关闭]
【发布时间】:2013-08-20 12:36:02
【问题描述】:

我做这个代码:

   `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 中的所有课程数据。 请我想访问选定的类或接口以反映所有成员数据。 谁来帮帮我

【问题讨论】:

    标签: c# .net visual-studio-2010 visual-studio-addins


    【解决方案1】:

    您可以按照here 的描述获取活动的Project,然后获取其ProjectItems,并为每个ProjectItem 获取其FileCodeModel,然后使用Kind= vsCMElementInterface 迭代其CodeElements 以获取那里定义的接口

    示例

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

    【讨论】:

    • 我做这个代码:object[] ps=(object[])_applicationObject.ActiveSolutionProjects; foreach (Project pr in ps) { ProjectItems _ProjectItems = pr.ProjectItems; CodeModel hh= _ProjectItems.ContainingProject.CodeModel; CodeElements els = hh.CodeElements; }我无法访问项目的类和接口
    • 我用一个工作示例更新了我的答案
    • 感谢 Stephan Bauer 它可以工作,但我可以访问类并获取它的类型吗:MethodInfo[] mi= obj.GetType().GetMethods(); // obj 我从选定的项目中获取它
    • 感谢 Stephan Bauer 它可以工作,但我可以访问类并获取它的类型来做到这一点:MethodInfo[] mi= obj.GetType().GetMethods(); // obj 我从选定的项目中获取它
    • 我认为您不应该在这里使用反射,而是修改上面的代码以获取类/接口中的方法(只需检查正确的Kind)。
    猜你喜欢
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多