API检查项目变量和参考路径在项目文件项目、变量、路径、文件

2023-09-04 04:09:54 作者:xx睡不着

我工作的一个 .NET应用程序(VS2010)随x没有。解决方案和可变没有。在这些解决方案的项目。 我需要检查,如果该项目的属性(具体以一定数量的项目)是同质的,也在生成过程中检查/验证的参考路径。 是否有一个 API 这样做吗?如果不是,我怎么去建设一个?

I am working on a .net application (VS2010) with x no. of solutions and variable no. of projects in those solutions. I need to check if the project properties (specific to a certain number of projects) are homogenous, and also check/validate the reference paths during the build. Is there an API that does this? If not, how do i go about building one?

推荐答案

您可以使用的MSBuild框架来做分析和项目文件进行评估。你需要使用的主要类 ProjectCollection 和< A HREF =htt​​p://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.project相对=nofollow>项目。

You can use MSBuild framework to do the parsing and perform evaluation of the project files. The main classes you need to use are ProjectCollection and Project.

但是,首先你需要处理你的.sln文件。由于API不能直接加载的.sln文件,则需要先在您的.sln文件转换为项目文件,该API可以加载。该方法是这里描述。您将获得转换后.sln.metaproj的文件,这是等价的重$ P $的.sln的psentation,但MSBuild的格式。之后,你可以分析.sln.metaproj文件和引用的项目和评估你需要的属性。此示例打印出OutputPath财产评估调试|解决方案中的所有项目中值为anycpu配置:

But first you will need to deal with your .sln files. Since API cannot load .sln files directly, you will need first to convert your .sln files to projects files, that API can load. The method is described here. You will get a .sln.metaproj files after conversion, that are equivalent representation of .sln, but have MSBuild format. After that you can parse .sln.metaproj files and referenced projects and evaluate properties you need. This sample prints out the OutputPath property evaluation for Debug|AnyCPU configuration of all projects in the solution:

    Dictionary<string, string> globalProperties = new Dictionary<string, string>();

    globalProperties.Add("Configuraion", "Debug");
    globalProperties.Add("Platform", "AnyCPU");

    ProjectCollection pc = new ProjectCollection(globalProperties);

    Project sln = pc.LoadProject(@"my_directory\My_solution_name.sln.metaproj", "4.0");

    foreach (ProjectItem pi in sln.Items)
    {
        if (pi.ItemType == "ProjectReference")
        {
            Project p = pc.LoadProject(pi.EvaluatedInclude);
            ProjectProperty pp = p.GetProperty("OutputPath");
            if (pp != null)
            {
                Console.WriteLine("Project=" + pi.EvaluatedInclude + " OutputPath=" + pp.EvaluatedValue);
            }
        }
    }