Skip to content

Commit 0c749fb

Browse files
authored
Add ability to get ProjectInstance objects (#41)
1 parent 9b3ef8c commit 0c749fb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/MSBuildProjectCreator.UnitTests/ProjectTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ namespace Microsoft.Build.Utilities.ProjectCreation.UnitTests
1212
{
1313
public class ProjectTests : MSBuildTestBase
1414
{
15+
[Fact]
16+
public void ProjectInstanceGetsCorrectObject()
17+
{
18+
ProjectCreator.Create()
19+
.Property("FA7AA8A112DA480AA2591A7FF619B05A", "43EEA58D938C4C6A9060816155FD5FA9")
20+
.ProjectInstance
21+
.GetPropertyValue("FA7AA8A112DA480AA2591A7FF619B05A")
22+
.ShouldBe("43EEA58D938C4C6A9060816155FD5FA9");
23+
}
24+
1525
[Fact]
1626
public void ProjectIsReEvaluated()
1727
{

src/MSBuildProjectCreator/ProjectCreator.Project.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the MIT license.
44

55
using Microsoft.Build.Evaluation;
6+
using Microsoft.Build.Evaluation.Context;
7+
using Microsoft.Build.Execution;
68
using System;
79
using System.Collections.Generic;
810

@@ -20,6 +22,11 @@ public partial class ProjectCreator
2022
/// </summary>
2123
private Project _project;
2224

25+
/// <summary>
26+
/// Stores the <see cref="ProjectInstance" /> for the current project.
27+
/// </summary>
28+
private ProjectInstance _projectInstance;
29+
2330
/// <summary>
2431
/// Gets the <see cref="Project"/> instance for the current project. The project is re-evaluated if necessary every time this property is accessed.
2532
/// </summary>
@@ -38,6 +45,11 @@ public Project Project
3845
}
3946
}
4047

48+
/// <summary>
49+
/// Gets the <see cref="ProjectInstance" /> for the current project.
50+
/// </summary>
51+
public ProjectInstance ProjectInstance => _projectInstance ?? (_projectInstance = Project.CreateProjectInstance());
52+
4153
/// <summary>
4254
/// Gets a <see cref="Project"/> instance from the current project.
4355
/// </summary>
@@ -97,5 +109,22 @@ public ProjectCreator TryGetProject(
97109

98110
return this;
99111
}
112+
113+
/// <summary>
114+
/// Gets a <see cref="ProjectInstance" /> from the current project.
115+
/// </summary>
116+
/// <param name="projectInstance">Receives the <see cref="ProjectInstance" />.</param>
117+
/// <param name="projectInstanceSettings">Optional <see cref="ProjectInstanceSettings" /> to use when creating the project instance.</param>
118+
/// <param name="evaluationContext">Optional <see cref="EvaluationContext" /> to use when creating the project instance.</param>
119+
/// <returns>The current <see cref="ProjectCreator"/>.</returns>
120+
public ProjectCreator TryGetProjectInstance(
121+
out ProjectInstance projectInstance,
122+
ProjectInstanceSettings projectInstanceSettings = ProjectInstanceSettings.None,
123+
EvaluationContext evaluationContext = null)
124+
{
125+
projectInstance = Project.CreateProjectInstance(projectInstanceSettings, evaluationContext);
126+
127+
return this;
128+
}
100129
}
101130
}

0 commit comments

Comments
 (0)