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

查看:17
本文介绍了如何使用 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 添加到项目中,并且位于 Libsx86 子文件夹中

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  

它们已使用 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 的 Build Action 设置为Embedded Resource".

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>Libsx86My1st.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>Libsx86My1st.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天全站免登陆