如何使用 Visual Studio Automation 设置活动项目?

发布于 2024-10-01 09:13:12 字数 504 浏览 0 评论 0原文

语境: 我正在使用 Visual Studio 2010 构建一个外接程序。其中一项操作是确保相关项目上存在界面。如果接口不存在,则必须创建它。

检查接口是否存在,不是问题。创建界面是。 我想使用 AddNewItem() 创建界面,但这只能在当前活动项目中正常工作。

代码:

ProjectItem item = VsProject.ProjectItems.Cast< ProjectItem >( ).FirstOrDefault( p => p.Name == interfaceName );
if ( item == null )
{
   item = VsProject.ProjectItems.DTE.ItemOperations.AddNewItem( @"Visual C# Items\Code\Interface", interfaceName+".cs" );
}

有人有想法吗? PS 需要明确的是:该加载项是从同一解决方案中的不同项目调用的。

Context:
I am building an Add-in using visual studio 2010. One of the actions is to ensure an Interface exists on a related project. If the interface does not exist, it must be created.

Checking if the interface exists, it not the problem. Creating the interface is.
I would like to create the interface using AddNewItem(), but this will only work properly on the current active project.

code:

ProjectItem item = VsProject.ProjectItems.Cast< ProjectItem >( ).FirstOrDefault( p => p.Name == interfaceName );
if ( item == null )
{
   item = VsProject.ProjectItems.DTE.ItemOperations.AddNewItem( @"Visual C# Items\Code\Interface", interfaceName+".cs" );
}

Has anybody an idea?
P.S. To be clear: the Add-in is called from a different project in the same solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-10-08 09:13:12

我想你会想使用 Project.ProjectItems.AddFromTemplate() 代替。轻松获得正确的项目参考。

I think you'll want to use Project.ProjectItems.AddFromTemplate() instead. No trouble getting the right Project reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文