Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用AddNamedCommand 2在Visual Studio 2012中注册命令_C#_Visual Studio 2012_Visual Studio Addins - Fatal编程技术网

C# 无法使用AddNamedCommand 2在Visual Studio 2012中注册命令

C# 无法使用AddNamedCommand 2在Visual Studio 2012中注册命令,c#,visual-studio-2012,visual-studio-addins,C#,Visual Studio 2012,Visual Studio Addins,我目前正在将以前的VBE宏移植到Visual Studio 2012。由于VBE不再可用,您需要实现自定义加载项。到目前为止没有问题 在这里,我创建了一个C加载项,实现了IDTExtensibility2和IDTCommandTarget。 在OnConnection方法中,我注册了应该在visualstudio启动时由 devenv [parameters...] /Command:AddIn.Connect.MakeMeProud 遇到各种问题后,我实现了一个助手例程,它在Visual S

我目前正在将以前的VBE宏移植到Visual Studio 2012。由于VBE不再可用,您需要实现自定义加载项。到目前为止没有问题

在这里,我创建了一个C加载项,实现了IDTExtensibility2和IDTCommandTarget。 在OnConnection方法中,我注册了应该在visualstudio启动时由

devenv [parameters...] /Command:AddIn.Connect.MakeMeProud
遇到各种问题后,我实现了一个助手例程,它在Visual Studio中注册命令:

    private const String CommandMain = "AddIn";

    private void registerCommand(String Name, ref object[] ctguid)
    {
        String cmd = CommandMain + ".Connect." + Name;

        int disableFlags = (int)vsCommandStatus.vsCommandStatusSupported +
                (int)vsCommandStatus.vsCommandStatusEnabled;

         _applicationObject.Commands.AddNamedCommand(_addInInstance, cmd, Name, Name, false, 0, ref ctguid, disableFlags);
    }

    private void registerCommands()
    {
        object[] contextGUIDS = new object[]{};
        registerCommand("MakeMeProud", ref contextGUIDS);
    }

    private void unregisterCommands()
    {
        foreach (Command cmd in (_applicationObject.Commands))
        {
            try
            {
                if (cmd.Name.StartsWith(CommandMain + ".Connect."))
                    cmd.Delete();
            }
            catch { }
        }
    }
删除现有命令后,在OnConnection中调用函数registerCommands:

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;

        if ((ext_ConnectMode.ext_cm_AfterStartup == connectMode) ||
             (ext_ConnectMode.ext_cm_Startup == connectMode) ||
             (ext_ConnectMode.ext_cm_CommandLine == connectMode))
        {
            unregisterCommands();
            registerCommands();
        }                    
    }
但是当我加载加载项时,Visual Studio会在调用AddNamedCommand时抱怨System.InvalidArgumentException,在使用AddNamedCommand 2时也会发生同样的情况

有什么建议吗

我不想在工具中有任何条目,只是从命令行和命令窗口运行它

发现错误

您只需按名称注册命令,不需要类。在Query和Exec中,也比较类名

错误消息并不是很有帮助,但是使用调试器检查另一个加载项帮助很大

干杯