开发者

How can I get a collection of the assemblies in my .NET solution?

For a .NET solution, is there a way to get all the .dll files for each project in the solution? For example, if I make an empty .NET solution (this is Visual Studio 2010 btw), and add 3 projects called "a", "b" and "c", and build each, providing me with dll's a.dll, b.dll, and c.dll, is there any code sample in either C# or P开发者_Go百科owershell which could give me a collection of of these .dlls (but not any other .dlls like 3rd party libraries used)?

Thanks


It's difficult to implement a 100% robust solution for this scenario. Project files, while usually simple, can get very complex as people take advantage of the flexibility of MSBuild. It's possible for instance for the same project to build assemblies with very different names given specific build settings. This makes it very difficult to pick out the assembly name without understanding the overall build context.

However for the most straight forward cases the following powershell script will do the trick. Make sure that it's run in the same directory as the specified solution file.

gc SomeProject.sln |
    ? { $_ -match "^Project" } |
    % { ($_.Split(","))[1].Trim().Trim('"') } |
    ? { $_ -match ".*proj" } |
    % { $x = [xml](gc $_); $x.Project.PropertyGroup[0].AssemblyName } |
    % { $_ + ".dll" }


One simple way (without writing a VS addin) would be parsing the project file. The project file (which is in XML format) will have an element called ProjectReference (regular references are in another element, called Reference):

<Project ...>
  <ItemGroup>
    <ProjectReference Include="..\path\YourProject.csproj">
      ...
    </ProjectReference>
  </ItemGroup>
</Project>

You could simply parse this, with any XML API you have in your toolset and get the name of the dll by using the name of the project (the Include attribute). This works of course only if your project has the same name as the dll. If it hasn't, you could open the csproj file and find the element <AssemblyName>


what about using the EnvDTE namespace? A related example is shown here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜