我有一个带有测试项目的.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).
发布评论
评论(1)
我认为资源名称由项目的
rootNamespace
而不是其defaultNamespace
,因此这似乎在.fsproj文件中起作用:这是由
createfsharpmanifestresourcename
构建任务。I think the resource name is qualified by the project's
RootNamespace
, rather than itsDefaultNamespace
, so this seems to work in the .fsproj file:This is implemented by the
CreateFSharpManifestResourceName
build task.