如何使用MSBuild更改嵌入式资源的默认名称空间? [英] How to change the default namespace for Embedded Resources with MSBuild?

查看:90
本文介绍了如何使用MSBuild更改嵌入式资源的默认名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将非托管dll嵌入我的控制台项目中.项目的默认名称空间为Company.Project1Exe.程序集名称(输出exe)命名为project1.exe

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

使用Add as Link选项将dll添加到项目中,并且位于Libs\x86子文件夹

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  

它们已经使用Add as Link选项添加到了项目中,因此实际上不在Libs子文件夹中.

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

我已将这两个dll的生成操作"设置为嵌入式资源".

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

默认情况下,MSBuild将使用DefaultNamspace.ExtendedNamespace.FileName嵌入这些dll,其中ExtendedNamespace代表项目的目录结构.

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

这导致资源分别作为Company.Project1.Libs.x86.My1st.dllCompany.Project1.Libs.x86.My2nd.dll嵌入.

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

我希望使用程序集名称嵌入这些资源,以便分别将它们嵌入为Project1.Libs.x86.My1st.dllProject1.Libs.x86.My2nd.dll.

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.

我该怎么做?

推荐答案

可以解决此问题的一种方法是设置嵌入式资源的LogicalName.默认情况下,嵌入资源时,您会在csproj文件中找到类似于

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"/>

对于使用Add as Link添加的资源,您会发现一个附加的Link属性.在这种情况下,Link属性是资源相对于您的项目结构的路径,而Include属性是指向文件在计算机上(相对于您的项目)的位置.

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>

为了使用不同的命名空间嵌入程序集,可以在上面添加LogicalName属性,该属性允许覆盖默认的msbuild行为.

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>

不利的一面是,每个添加的资源都需要这样做.但是,我更希望以某种方式设置此约定,以便可以将其作为在我的项目中嵌入任何资源的默认方式,即使用$(TargetName)代替默认名称空间

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

这篇关于如何使用MSBuild更改嵌入式资源的默认名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆