Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

visual studio 2010 - 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 Libsx86 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>Libsx86My1st.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>Libsx86My1st.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...