Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the default namespace for Embedded Resources with MSBuild?

I am attempting to embed an unmanaged dll in my console project. The default namespace of the project is Company.Project1Exe. The Assembly Name (output exe ) is named project1.exe

The dlls are added to the project using the Add as Link option and are located in a Libs\x86 subfolder

Company.Project1Exe
   |
   |--Program.cs
   |--Libs
       |--x86
           |-My1st.dll
           |-My2nd.dll  

They have been added to the project using the Add as Link option, thus are not physically locate in the Libs subfolder.

I have set the Build Action of both these dlls to 'Embedded Resource'.

By default, MSBuild will embed these dlls using the DefaultNamspace.ExtendedNamespace.FileName where the ExtendedNamespace represents the directory structure of the project.

This results in resource being embedded as Company.Project1.Libs.x86.My1st.dll and Company.Project1.Libs.x86.My2nd.dll respectively.

I want these resources to embedded using the Assembly Name so that they are embedded as Project1.Libs.x86.My1st.dll and Project1.Libs.x86.My2nd.dll respectively.

How can I do this?

like image 810
Ahmad Avatar asked May 23 '12 06:05

Ahmad


1 Answers

The one method that can address this issue is to set the LogicalName of the embedded resource. By default when you embed a resource, you will find an entry in your csproj file similar to

<EmbeddedResource Include="path to embdedded resource"/>

In the case of resources that are added using Add as Link, you will find an additional Link attribute. In this case, the Link attribute is the path of the resource relative to your project structure and the Include attribute is the pointing to file's location on your machine (relative to your project).

<EmbeddedResource Include="path to embdedded resource"/>
  <Link>Libs\x86\My1st.dll</Link>
</EmbeddedResource>

In order to get the assemblies embedded using a different namespace the LogicalName attribute can be added to the above which allows one to override the default msbuild behaviour.

<EmbeddedResource Include="path to embdedded resource"/>
  <Link>Libs\x86\My1st.dll</Link>
  <LogicalName>$(TargetName).Libs.x86.My1st.dll</LogicalName>
</EmbeddedResource>

The downside it would seem, is that one will need to do this for every resource added. I would however have preferred that this convention be set in some way such that this can be the default way to embed any resource in my project i.e. use the $(TargetName) as a replacement for the default namespace

like image 193
Ahmad Avatar answered Oct 23 '22 17:10

Ahmad