Is it possible to get all the project references that my project has? So for instance, my Project A has a reference to Project B and Project C. I don't want to get references for everything like libraries and such, just other projects in my solution. I need it in code so I can save it in a database.
You can use classes from Microsoft.Build.Evaluation
to help with this.
Specifically, the ProjectCollection
class. To use this you need to add the following references to your project:
Microsoft.Build
Microsoft.Build.Utilities.Core
(When adding these via the Reference Manager , look in Assemblies -> Extensions, otherwise you might reference an old version which doesn't work with newer project files.)
Then you can write code such as the following to iterate through all the project references:
using System;
using Microsoft.Build.Evaluation;
namespace Demo
{
class Program
{
static void Main()
{
var projectCollection = new ProjectCollection();
var projFile = @"E:\Test\CS7\ConsoleApp1\ConsoleApp1.csproj";
var project = projectCollection.LoadProject(projFile);
var projectReferences = project.GetItems("ProjectReference");
foreach (var projectReference in projectReferences)
{
Console.WriteLine(projectReference.EvaluatedInclude);
}
}
}
}
You can just right click on each project and navigate to the BuildDependencies > ProjectDependencies.
Assuming you're not trying to write an external project analyzer/dependency graph creator.
Update as per comment: If you are doing a static code analyzer (go through the files in the solution) you can iterate the .csproj files and extract the parts like the following:
<ItemGroup>
<ProjectReference Include="..\xyz.Service.Client\xyz.Service.Client.csproj" />
<ProjectReference Include="..\xyz.Service.Interface\xyz.Service.Interface.csproj" />
<ProjectReference Include="..\xyz.Web.Interface\xyz.Web.Interface.csproj" />
</ItemGroup>
You can map it to a desired dto structure and save it as you wish. Here is some code that might ease the solution:
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(fullProjectPath);
IEnumerable<string> references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Select(refElem => refElem.Value);
foreach (string reference in references)
{
Console.WriteLine(reference);
}
Unfortunately, there is no "IsPartOfSolution"-Flag you can check.
But it is relatively easy to condense the full list down:
IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.Contains("SolutionName"));
Sounds like a code analyzer to me,
If you start a new 'Stand-Alone Code Analysis tool' project a pretty complete sample project will be generated for you. I'm not quite sure how far it goes, but you end up with a SolutionLoader
object.
Iterate over the loader.Solution.Projects
to get all projects in your solution. Each Project
has a ProjectId
and a property AllProjectReferences
(which includes references outside your project).
Filtering these by projectId's which are included in your solution shoulg get you on your way.
I have this code in vb.net that could fit your requirements:
Private Sub GetProjectReferences()
Dim lines = New List(Of String)
Dim path = "..\..\TestApp.vbproj"
For Each line In File.ReadAllLines(path)
If line.Contains("<ProjectReference") Then
Dim projNameWithExtension = line.Substring(line.LastIndexOf("\") + 1)
Dim projName = projNameWithExtension.Substring(0, projNameWithExtension.IndexOf(".vbproj"))
lines.Add(projName)
End If
Next
End Sub
If you translate it to c# (function and variable definitions, and .vbproj to .csproj) it could be of some use
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.