[英]Changing Project Metadata in Visual Studio 2013 C#
I've got a Visual Studio 2013 package written in C# where I need to make some changes in the items and their attributes in a C++ project file while Visual Studio 2013 is running and has the project loaded. 我有一个用C#编写的Visual Studio 2013包,我需要在Visual Studio 2013运行并加载项目时对C ++项目文件中的项及其属性进行一些更改 。 Actually, it could also be OK to save the project file at the point when the whole solution is being saved.
实际上,在保存整个解决方案时保存项目文件也可以。
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="Test">
<MyItemToChange Include = "TestItem">
<MyMetadata1ToChange>Value1</MyMetadata1ToChange>
<MyMetadata2ToChange>Value2</MyMetadata2ToChange>
</MyItemToChange>
</ItemGroup>
<!-- The rest of the file -->
</Project>
In previous Visual Studio versions I used the Microsoft.Build.Evaluation.Project class to change the items and the metadata of the project. 在以前的Visual Studio版本中,我使用Microsoft.Build.Evaluation.Project类来更改项目的项目和元数据。 When you have the reference to the project, you can either accessed the XML structure of the project directly through
Project.Xml
property or get a hold on different Microsoft.Build.Evaluation.ProjectItem
and use the interfaces found there. 当您拥有对项目的引用时,您可以直接通过
Project.Xml
属性访问项目的XML结构,或者获取不同的Microsoft.Build.Evaluation.ProjectItem
并使用在那里找到的接口。
However, since Microsoft has changed the structure of Visual Studio in their 2013 release, though the Microsoft.Build.Evaluation.Project
is not deprecated, you can't access the C++ projects any more through Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects as the collection is empty. 但是,由于Microsoft已在2013版本中更改了Visual Studio的结构,但不推荐使用
Microsoft.Build.Evaluation.Project
,因此无法通过Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection访问C ++项目。 LoadedProjects作为集合是空的。 However it still works if you want to access C# projects. 但是,如果要访问C#项目,它仍然有效。 (As a matter of fact, browsing deep into the objects while debugging you can find that they still use the same
Microsoft.Build.Evaluation
namespace for inner representation.) (事实上,在调试时深入浏览对象可以发现它们仍然使用相同的
Microsoft.Build.Evaluation
命名空间进行内部表示。)
Since I can't access the Microsoft.Build.Evaluation.Project
any more, I need an alternative to 由于我无法再访问
Microsoft.Build.Evaluation.Project
,我需要替代
Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects
Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.LoadedProjects
之外,访问项目 Unless I've missed something, the following are not good for my purposes: 除非我错过了什么,否则以下内容对我的目的不利:
Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetItemAttribute
sets only attributes to items that are displayed in the Solution Explorer. Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetItemAttribute
仅将属性设置为“解决方案资源管理器”中显示的项目。 "MyItemToChange" is not displayed in the Solution Explorer Microsoft.Build.Evaluation.Project
, and Microsoft.Build.Evaluation.ProjectItem
, but first of all, it is quite unsafe for future uses and as far as I remember, you cannot be sure that if you modify these now internal classes, the project will know that it is dirty and has to be saved. Microsoft.Build.Evaluation.Project
和Microsoft.Build.Evaluation.ProjectItem
,但首先,它对未来非常不安全使用并且据我记得,你不能确定如果你现在修改这些内部类,项目将知道它是脏的并且必须保存。 I'm glad to have any suggestions as I'm really running out of options. 我很高兴有任何建议,因为我真的没有选择。
Thanks. 谢谢。
Well, I have worked out a workaround , not a real solution for the problem. 好吧,我已经找到了解决方法 ,而不是问题的真正解决方案。
What I do right now is the following: 我现在做的是以下内容:
EnvDTE.DTE.ItemOperations.PromptToSave
EnvDTE.DTE.ItemOperations.PromptToSave
EnvDTE.DTE.SourceControl.CheckOutItem()
method. EnvDTE.DTE.SourceControl.CheckOutItem()
方法在源代码管理中检查项目文件。 If it throws an exception (NotImplementedException), I use a bodge to check out the project file. Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetProperty()
method. Microsoft.VisualStudio.Shell.Interop.IVsBuildPropertyStorage.SetProperty()
方法将自定义属性写入项目文件。 EnvDTE.DTE.Solution.Close()
System.Xml.XmlDocument
class. System.Xml.XmlDocument
类打开项目文件。 Edit. EnvDTE.DTE.Solution.Open()
At this point the user might be asked again to confirm some source control options. EnvDTE.DTE.Solution.Open()
此时可能会再次要求用户确认某些源控制选项。 Why is it just a workaround not a proper solution? 为什么它只是一种解决方法而不是一个合适的解决方案
I'm still looking for the proper solution for the problem. 我仍在为这个问题寻找合适的解决方案。 But until someone could tell me how it is done, I have to live with this current implementation.
但是,除非有人能告诉我它是如何完成的,否则我必须忍受当前的实施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.