文件嵌套时,嵌入式资源名称丢失扩展名 [英] Embedded Resource Name Loses Extension When File is Nested

查看:87
本文介绍了文件嵌套时,嵌入式资源名称丢失扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些脚本存储在标记为嵌入式资源"的文件中.我将这些文件中的每一个嵌套在其关联的 .cs 文件下.不幸的是,由于某种原因,当您以这种方式嵌套文件时,嵌入式资源名称将失去文件扩展名.这意味着在运行时,我无法确定哪些嵌入式资源是脚本或不是脚本.我该怎么办?

I have some scripts stored in files that I've marked as Embedded Resource. I nest each of these files under their associated .cs file. Unfortunately, for some reason, when you nest a file this way, the embedded resource name loses the file extension. This means that at runtime I have no way to identify which embedded resources are or aren't scripts. What can I do about this?

我尝试过的一件事不起作用:获取 ManifestResourceInfo 对象,该对象具有 FileName 属性.不幸的是,此属性为 null .

One thing I tried that did not work: getting the ManifestResourceInfo object, which has a FileName property. Unfortunately this property is null.

我尝试的另一件事是将扩展名加倍.(例如: filename.ext.ext ).这不起作用,并且第一个.之后的所有内容仍然丢失.

Another thing I tried was doubling up the extension. (like: filename.ext.ext). This didn't work, and everything after the first . is still missing.

我尝试使用非常不同的名称来命名资源,并注意到资源名称甚至都没有改变.似乎正在基于依赖"文件生成嵌套的嵌入式资源文件的资源名称,在这种情况下,该文件只是常规的 .cs 文件.我尝试将 .cs 文件的扩展名加倍为 filename.extrastuff.cs ,但是资源名称仍然没有改变.它坚持在第一个..

I tried naming the resource with something very different, and noticed that the resource name didn't even change. It seems that it is generating the resource name for a nested embedded resource file based off of the "dependent upon" file, which in this case is just a regular .cs file. I tried doubling the extension of the .cs file to filename.extrastuff.cs but the resource name still doesn't change. It insists on clipping everything after the first ..

不,好的,我现在看到它实际上是在 .cs 文件中定义的 type 之后命名资源,而不是两个文件的文件名.这就是为什么扩展名没有区别的原因.这意味着我无法对这两个文件名进行任何操作来帮助查找资源.

No, ok, I see now that it is actually naming the resource after the type defined in the .cs file, not the filename of either file. This is why the extension makes no difference. This means there is nothing I can do to either filename to help find the resource.

推荐答案

我发现文件丢失扩展名的原因是由于某种原因,当文件嵌套时,VS在 type <之后命名资源./em>,而不是在文件之后.

I discovered that the reason the file loses its extension is because for some reason when the file is nested, VS is naming the resource after the type instead of after the file.

我发现唯一让我在嵌套时仍具有扩展名的事情是手动编辑 .csproj 文件并添加 LogicalName 标记,该标记允许您可以覆盖资源名称.

The only thing I've found to allow me to still have an extension when nesting is to manually edit the .csproj file and add a LogicalName tag, which allows you to override the resource name.

<EmbeddedResource Include="Path\To\NestedFile.ext">
  <LogicalName>NestedFile.ext</LogicalName>
  <DependentUpon>ParentFile.cs</DependentUpon>      
</EmbeddedResource>

不幸的是,对此没有IDE支持,而我正在使用的FileNesting扩展也不支持编写此标记.

Unfortunately, there is no IDE support for this, and the FileNesting extension I'm using doesn't support writing this tag either.

必须为每个文件执行此操作太麻烦了,因此我不必依靠文件扩展名,而必须做一些事情,例如在脚本文件中添加一个将其标识为脚本的标识符.

It's too cumbersome to have to do this for every file, so instead of relying on the file extension, I would have to do something like add an identifier inside my script files that identifies them as scripts.

我最终意识到,由于我是在验证单元测试中的脚本文件,因此我可以简单地扫描文件系统而不是资源清单,从而完全避免出现此问题.

Ultimately I realized that since in my case I'm validating script files from unit tests, I can simply scan the file system instead of the resource manifest and avoid this problem altogether.

这篇关于文件嵌套时,嵌入式资源名称丢失扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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