The Solution
object represents the currently loaded solution. The individual projects within the solution are available via Project
objects returned within the Solution.Projects
collection. Items within a project are accessed in a similar fashion through the Project.ProjectItems
collection.
As you can see from Figure 14.1, this hierarchy exactly mirrors the solution/project hierarchy that we first discussed in Chapter 4, “Solutions and Projects.”
There are some mismatches here (solution folders, for instance, are treated as projects), but for the most part, the object model tree closely resembles the solution project tree that you are used to.
The Solution
object and members enable you to interact with the current solution to perform common tasks such as these:
Determining the number of projects in the solution (Count
property)
Adding a project to the solution based on a project file (AddFromFile
method)
Creating a new solution or closing the current one (Create
and Close
methods)
Saving the solution (SaveAs
method)
Removing a project from the solution (Remove
method)
You can directly retrieve a reference to any of the projects within the currently loaded solution by iterating over the Solution.Projects
collection. As an example of interacting with the Solution
and Project
objects, this C# code snippet removes the first project from the current solution.
Solution solution = _applicationObject.Solution;
Project project = solution.Projects.Item(0) as Project;
if (project.Saved)
{
solution.Remove(project);
}
else
{
//
}
Note
Most of the code snippets you see in this chapter are merely meant to reinforce how you would access the API component being discussed. We get into the specifics of exactly how to leverage these concepts within the next chapter. If you want to get a head start by playing with the code as we go along, you need to skip ahead to Chapter 15, create a Visual Studio package project, and then return here to follow along.
Table 14.3 provides the combined list of the most commonly used properties and methods implemented by the Solution
objects.