C# 在VS2010中添加新项目后,右键单击项目时,我想访问选定的类或接口

C# 在VS2010中添加新项目后,右键单击项目时,我想访问选定的类或接口,c#,.net,visual-studio-2010,visual-studio-addins,C#,.net,Visual Studio 2010,Visual Studio Addins,我执行以下代码: `public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst;

我执行以下代码:

   `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");                
        }`
我正在开发插件以反映VS2010中类的所有数据。 请允许我访问所选类或接口以反映所有成员数据。 任何一个help me

您都可以按描述获取活动的,然后获取每个its的和,然后使用
=vsCMElementInterface
迭代its以获取其中定义的接口

示例

// 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 ) );
        }
    }
}
//结果的容器
列表类=新列表();
列表接口=新列表();
//从解决方案资源管理器获取所选项目
数组项目=(数组)\u applicationObject.ActiveSolutionProjects;
//获取选定项目中的所有项目项
var projectItems=项目
.第()类
.Where(p=>p.ProjectItems!=null)
.SelectMany(p=>p.ProjectItems.OfType()。其中(pi=>pi.FileCodeModel!=null));
//迭代所有这些项目
foreach(ProjectItem ProjectItem in projectItems)
{
//获取当前项目项中的所有代码元素(接口和类)(递归)
var elements=projectItem.FileCodeModel.CodeElements
.第()类
.SelectMany(ce=>this.GetCodeElements(ce));
//对找到的代码元素执行一些操作
classes.AddRange(elements.Where(el=>el.Kind==vsCMElement.vsCMElementClass).Select(el=>el.Name));
interfaces.AddRange(elements.Where(el=>el.Kind==vsCMElement.vsCMElementInterface).选择(el=>el.Name));
}
//GetCodeElements的可能实现:
私有IEnumerable GetCodeElements(CodeElement根)
{
列表结果=新列表();
if(root==null)
返回结果;
//如果当前CodeElement是接口或类,请将其添加到结果中
if(root.Kind==vsCMElement.vsCMElementClass | | root.Kind==vsCMElement.vsCMElementInterface)
{
结果:添加(根);
}
//递归检查子项
if(root.Children!=null&&root.Children.Count>0)
{
foreach(root.Children.OfType()中的var项)
{
result.AddRange(this.GetCodeElements(项));
}
}
}
您可以按照描述获取活动的,然后获取每个its的和,然后使用
=vsCMElementInterface
迭代its,以获取在其中定义的接口

示例

// 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 ) );
        }
    }
}
//结果的容器
列表类=新列表();
列表接口=新列表();
//从解决方案资源管理器获取所选项目
数组项目=(数组)\u applicationObject.ActiveSolutionProjects;
//获取选定项目中的所有项目项
var projectItems=项目
.第()类
.Where(p=>p.ProjectItems!=null)
.SelectMany(p=>p.ProjectItems.OfType()。其中(pi=>pi.FileCodeModel!=null));
//迭代所有这些项目
foreach(ProjectItem ProjectItem in projectItems)
{
//获取当前项目项中的所有代码元素(接口和类)(递归)
var elements=projectItem.FileCodeModel.CodeElements
.第()类
.SelectMany(ce=>this.GetCodeElements(ce));
//对找到的代码元素执行一些操作
classes.AddRange(elements.Where(el=>el.Kind==vsCMElement.vsCMElementClass).Select(el=>el.Name));
interfaces.AddRange(elements.Where(el=>el.Kind==vsCMElement.vsCMElementInterface).选择(el=>el.Name));
}
//GetCodeElements的可能实现:
私有IEnumerable GetCodeElements(CodeElement根)
{
列表结果=新列表();
if(root==null)
返回结果;
//如果当前CodeElement是接口或类,请将其添加到结果中
if(root.Kind==vsCMElement.vsCMElementClass | | root.Kind==vsCMElement.vsCMElementInterface)
{
结果:添加(根);
}
//递归检查子项
if(root.Children!=null&&root.Children.Count>0)
{
foreach(root.Children.OfType()中的var项)
{
result.AddRange(this.GetCodeElements(项));
}
}
}

I执行以下代码:
object[]ps=(object[])\u applicationObject.ActiveSolutionProjects;foreach(ps中的项目pr){ProjectItems_ProjectItems=pr.ProjectItems;CodeModel hh=_ProjectItems.ContainingProject.CodeModel;CodeElements els=hh.CodeElements;}
我无法访问projectI的类和接口我用一个工作示例更新了我的答案谢谢Stephan Bauer它可以工作,但我可以访问类并获取它的类型来实现这一点:
MethodInfo[]mi=obj.GetType().GetMethods()//obj我从所选项目中获取它谢谢Stephan Bauer它可以工作,但我可以访问类并获取其类型来实现这一点:MethodInfo[]mi=obj.GetType().GetMethods();//obj我从所选项目中获得它我认为您不应该在这里使用反射,而是修改上面的代码以获得类/接口中的方法(只需检查正确的
种类
)。我执行以下代码:
object[]ps=(object[])\u applicationObject.ActiveSolutionProjects;foreach(ps中的项目pr){ProjectItems_ProjectItems=pr.ProjectItems;CodeModel hh=_ProjectItems.ContainingProject.CodeModel;CodeElements els=hh.CodeElements;}
我无法访问projectI的类和接口我用一个工作示例更新了我的答案谢谢Stephan Bauer它可以工作,但我可以访问类并获取它的类型来实现这一点:
MethodInfo[]mi=obj.GetType().GetMethods()//obj我从所选项目中获取它谢谢Stephan Bauer它可以工作,但我可以访问类并获取其类型来实现这一点:MethodInfo[]mi=obj.GetType().GetMethods();//obj我从选定的项目中得到它我认为你不应该使用反射他