-->

在C#编程添加经由EnvDTE项目引用(相对于装配参考)(Adding programmatical

2019-09-19 16:16发布

In visual studio when you add a reference to an existing project in your solution in the .csproj file it ends up like so:

<ProjectReference Include="..\TestProject2\TestProject2.csproj">
  <Project>{15EC8369-B0C5-4F71-A366-19042F450A2D}</Project>
  <Name>TestProject2</Name>
</ProjectReference>

If I add a reference to an assembly DLL via EnvDTE:

var p = _project as VsProject;
p.References.Add(<path to assembly DLL>);

it ends like this:

<Reference Include="TestProject2.csproj">
  <HintPath>..\TestProject2\bin\Debug\TestProject2.csproj.dll</HintPath>
</Reference>

This is not so great because if I switch to a Release build it will still reference the debug assembly. Another problem is that I have to build the referenced assembly before I can add it as a reference. With Visual Studio UI I can add a reference to an unbuilt project.

Is it possible via the EnvDTE API to add a project reference?

I know I can manipulate the .csproj file as an XML document and do whatever I want, but since I started on the path on EnvDTE I would prefer to stick to it.

Answer 1:

它看起来像引用接口有AddProject它处理项目到项目refrences方法。



Answer 2:

什么工作对我来说是制定参考这种方式:

<Reference
  Include="MyDll"
  Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <HintPath>..\..\somePath\Debug\myDll.dll</HintPath>
</Reference>
<Reference
  Include="MyDll"
  Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  <HintPath>..\..\somePath\Release\myDll.dll</HintPath>
</Reference>

通过这种方式,释放建设引用释放的依赖,和调试构建引用调试。 当然,在x86 / x64也可以在必要时进行处理。 这是一个32位的应用程序。



文章来源: Adding programmatically in C# a project reference (as opposed to an assembly reference) via EnvDTE