.NET-从汇编获得嵌入式资源路径

发布于 2025-02-13 01:57:55 字数 1823 浏览 0 评论 0 原文

我有一个带有测试项目的.net6 f#解决方案。
测试项目位于一个名为“ myproject.core.tests”的文件夹中,项目文件是 core.unittests.fsproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6</TargetFramework>
    <AssemblyName>UnitTests.Core</AssemblyName>
    <!--DefaultNamespace>UnitTests.Core</DefaultNamespace-->
  </PropertyGroup>
  <ItemGroup>
    <EmbeddedResource Include="aaa.txt"  />

在解决方案中,f#项目显示为“ core.unittests”。这是mySolution.sln条目:

Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Core.UnitTests", "MyProject.Core.Tests\Core.UnitTests.fsproj", "{027C4CE5-B47B-4E4E-B7D2-477B4AB981F2}"
EndProject

在项目中,我在buildAction“嵌入式资源”中有一个文件“ aaa.txt”。

我尝试阅读它,

module MyModule
  
  let assembly = Assembly.GetExecutingAssembly()
  let assemblyName = assembly.GetName().Name
  let stream = assembly.GetManifestResourceStream($"{assemblyName}.aaa.txt")

但流是无效的。
如果我打印出“ assemblyName”,那是:“ unittests.core”。
如果我使用 assembly.getManifestResourCenames()获得 “ core.unittests.aaa.txt” 而是使用我获取所有资源。

为什么将资源存储在“ core.unittests.aaa.txt”中,如果汇编名称为“ uniteSts.core”?

看来嵌入式资源路径是使用项目文件名生成的。
我还尝试在项目文件中设置 defaultNamespace ,但没有任何更改。
如何从集会中获取资源的初始部分?

[编辑]

我发现了这个解决方法:

module MyModule
  
  let nspace = assembly.GetExportedTypes().[0].Namespace
  let stream = assembly.GetManifestResourceStream($"{nspace}.aaa.txt")

不确定如何以更干净的方式从f#module获取默认名称空间(该代码不在类型/类中)。

I've a .net6 F# solution with a test project.
The test project is in a folder named "MyProject.Core.Tests" and the project file is
Core.UnitTests.fsproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6</TargetFramework>
    <AssemblyName>UnitTests.Core</AssemblyName>
    <!--DefaultNamespace>UnitTests.Core</DefaultNamespace-->
  </PropertyGroup>
  <ItemGroup>
    <EmbeddedResource Include="aaa.txt"  />

In the solution the F# project is shown as "Core.UnitTests". This is the MySolution.sln entry:

Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Core.UnitTests", "MyProject.Core.Tests\Core.UnitTests.fsproj", "{027C4CE5-B47B-4E4E-B7D2-477B4AB981F2}"
EndProject

In the project I have a file "aaa.txt" with BuildAction "Embedded Resource".

I try to read it with

module MyModule
  
  let assembly = Assembly.GetExecutingAssembly()
  let assemblyName = assembly.GetName().Name
  let stream = assembly.GetManifestResourceStream(
quot;{assemblyName}.aaa.txt")

But stream is null.
If I print out "assemblyName" it is: "UnitTests.Core".
If I get all the resources within the Assembly using assembly.GetManifestResourceNames() I get "Core.UnitTests.aaa.txt" instead.

Why the resource is stored in "Core.UnitTests.aaa.txt" if the Assembly name is "UnitTests.Core" ?

It seems that the embedded resource path is generated using the project file name.
I also tried to set the DefaultNamespace in the project file but nothing changed.
How can I get the initial part of the resources from the Assembly?

[EDIT]

I found this workaround:

module MyModule
  
  let nspace = assembly.GetExportedTypes().[0].Namespace
  let stream = assembly.GetManifestResourceStream(
quot;{nspace}.aaa.txt")

Not sure how to get the default namespace in a more clean way from F# module (that code is not in a type/class).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2025-02-20 01:57:55

我认为资源名称由项目的 rootNamespace 而不是其 defaultNamespace ,因此这似乎在.fsproj文件中起作用:

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>UnitTests.Core</RootNamespace>
  </PropertyGroup>

这是由 createfsharpmanifestresourcename 构建任务。

I think the resource name is qualified by the project's RootNamespace, rather than its DefaultNamespace, so this seems to work in the .fsproj file:

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>UnitTests.Core</RootNamespace>
  </PropertyGroup>

This is implemented by the CreateFSharpManifestResourceName build task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文