我通过查看Microsoft.Common.targets和Microsoft.CSharp.targets文件,找出了用于对资源进行排序并为其命名的任务,从而以一种非常hacky但完美运行的方式解决了这个问题。
因此,我覆盖了CreateCSharpManifestResourceName任务,并设置了ManifestResourceName和LogicalName等内容,使其符合我的要求:
<!-- Overriding this task in order to set ManifestResourceName and LogicalName -->
<UsingTask TaskName="CreateCSharpManifestResourceName" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<ResourceFiles ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Required="true" />
<RootNamespace ParameterType="System.String"
Required="true" />
<PrependCultureAsDirectory
ParameterType="System.Boolean" />
<ResourceFilesWithManifestResourceNames
ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
foreach (ITaskItem item in ResourceFiles) {
var link = item.GetMetadata("Link").Replace("\\", "/");
link = "/" + link.TrimStart('/');
item.SetMetadata("ManifestResourceName", link);
item.SetMetadata("LogicalName", link);
}
ResourceFilesWithManifestResourceNames = ResourceFiles;
</Code>
</Task>
</UsingTask>
作为额外的好处,我嵌入的文件会以它们的实际路径作为资源名称(只用
\
替换成
/
),因此可以轻松使用
Assembly.GetManifestResourceStream(x)
查找它们,其中x可能是
/path.to/my.SMS.file
之类的内容。
然后,我用一个覆盖AssignCulture任务的函数代替了原来的任务,并返回没有任何文件具有任何语言环境,并且将
<WithCulture>false</WithCulture>
添加到这些文件中(这在我的情况下非常完美)。
<!-- Overriding this task to set WithCulture and sort them as not having culture -->
<UsingTask TaskName="AssignCulture" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Required="true" />
<AssignedFilesWithCulture
ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Output="true" />
<AssignedFilesWithNoCulture
ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
ITaskItem[] withCulture = new ITaskItem[Files.Length];
ITaskItem[] withoutCulture = new ITaskItem[Files.Length];
for (var i = 0; i < Files.Length; i++) {
ITaskItem item = Files[i];
var wc = item.GetMetadata("WithCulture");
if (wc == "") { item.SetMetadata("WithCulture", "False"); }
if (wc.ToLowerInvariant() == "true") {
withCulture[i] = item;
} else {
withoutCulture[i] = item;
}
var type = item.GetMetadata("Type");
if (type == "") { item.SetMetadata("Type", "Non-Resx"); }
}
AssignedFilesWithCulture = withCulture;
AssignedFilesWithNoCulture = withoutCulture;
</Code>
</Task>
</UsingTask>