我可以从 VBScript 启动一个新的隐藏的 Visual Studio 进程,并以编程方式驱动它,方法是:
Set DTE = CreateObject("VisualStudio.DTE.8.0")
DTE.DoStuff()
我如何在 C# 中执行此操作? (编辑:使用正确的类型,而不是该 VBScript 代码使用的通用 COM 对象。)
我已经试过了:
using EnvDTE;
...
DTE dte = new DTE();
但我得到“为 CLSID 为 {3C9CFE1E-389F-4118-9FAD-365385190329} 的组件检索 COM 类工厂失败”。
最佳答案
我找到了答案(感谢 Sebastiaan Megens 让我走上正轨):
[STAThread]
static void Main(string[] args)
{
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0", true);
DTE2 dte = (EnvDTE80.DTE2)System.Activator.CreateInstance(t, true);
// See http://msdn.microsoft.com/en-us/library/ms228772.aspx for the
// code for MessageFilter - just paste it in.
MessageFilter.Register();
dte.DoStuff();
dte.Quit();
}
public class MessageFilter : IOleMessageFilter
{
... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx
(STAThread 和 MessageFilter 的废话是“由于外部多线程应用程序和 Visual Studio 之间的线程争用问题”,无论那是什么意思。粘贴来自 http://msdn.microsoft.com/en-us/library/ms228772.aspx 的代码使其工作。)
关于c# - 以编程方式启动 Visual Studio; C# 相当于 VB 的 CreateObject ("VisualStudio.DTE.8.0"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1270365/