C# 应用程序无法识别卫星资源程序集
由于我们的应用程序中的 .resx 文件数量非常多,我创建了以下 MSBuild 脚本来将所有语言 .resx 文件编译为 .resource,然后将它们嵌入到卫星资源程序集中。
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Res Include = "Dialog\*.ja-JP.resx">
<Culture>ja-JP</Culture>
<Project>Dialog</Project>
</Res>
</ItemGroup>
<Target Name="Main">
<Message Text="$(destination)"/>
<CallTarget Targets="CompileResources" />
<CallTarget Targets="BuildSatelliteAssemblies" />
<CallTarget Targets="CopyToFolder" Condition="$(destination)!=''"/>
<CallTarget Targets="CleanUp" />
</Target>
<Target Name="CompileResources">
<GenerateResource Sources="@(Res)" PublicClass="true" >
<Output ItemName="Resources" TaskParameter="OutputResources"/>
</GenerateResource>
</Target>
<Target Name="BuildSatelliteAssemblies" DependsOnTargets="CompileResources">
<MakeDir Directories="%(Res.Culture)"/>
<AL OutputAssembly="%(Culture)\%(Project).resources.dll"
Version="0.0.0.0"
Culture="%(Culture)"
ProductName="%(Project)"
Title="%(Project)"
EmbedResources="@(Resources)"/>
</Target>
<Target Name="CopyToFolder" DependsOnTargets="BuildSatelliteAssemblies">
<MakeDir Directories="$(destination)\%(Res.Culture)"/>
<CreateItem Include="%(Res.Culture)\*.dll" AdditionalMetadata="Culture=%(Res.Culture)">
<Output ItemName="SatelliteAssemblies" TaskParameter="Include"/>
</CreateItem>
<Copy DestinationFolder="$(destination)\%(Culture)"
SourceFiles="@(SatelliteAssemblies)" />
</Target>
<Target Name="CleanUp">
<Delete Files="@(Resources)"/>
</Target>
</Project>
附属程序集似乎可以正确编译和嵌入,但是当我将它们与我的应用程序一起放置时,它们无法被识别,并且默认返回默认文化资源。如果我使用 Visual Studio 构建项目并使用它创建的程序集,它们可以正常加载。
我必须遗漏一些东西才能使应用程序识别我的外部构建的程序集。它们的名称都相同,大小也几乎相同。
Due to the extreme amount of .resx files in our application, I have created the following MSBuild script to compile all language .resx files into .resource, then embed them into satellite resource assemblies.
<Project DefaultTargets="Main" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Res Include = "Dialog\*.ja-JP.resx">
<Culture>ja-JP</Culture>
<Project>Dialog</Project>
</Res>
</ItemGroup>
<Target Name="Main">
<Message Text="$(destination)"/>
<CallTarget Targets="CompileResources" />
<CallTarget Targets="BuildSatelliteAssemblies" />
<CallTarget Targets="CopyToFolder" Condition="$(destination)!=''"/>
<CallTarget Targets="CleanUp" />
</Target>
<Target Name="CompileResources">
<GenerateResource Sources="@(Res)" PublicClass="true" >
<Output ItemName="Resources" TaskParameter="OutputResources"/>
</GenerateResource>
</Target>
<Target Name="BuildSatelliteAssemblies" DependsOnTargets="CompileResources">
<MakeDir Directories="%(Res.Culture)"/>
<AL OutputAssembly="%(Culture)\%(Project).resources.dll"
Version="0.0.0.0"
Culture="%(Culture)"
ProductName="%(Project)"
Title="%(Project)"
EmbedResources="@(Resources)"/>
</Target>
<Target Name="CopyToFolder" DependsOnTargets="BuildSatelliteAssemblies">
<MakeDir Directories="$(destination)\%(Res.Culture)"/>
<CreateItem Include="%(Res.Culture)\*.dll" AdditionalMetadata="Culture=%(Res.Culture)">
<Output ItemName="SatelliteAssemblies" TaskParameter="Include"/>
</CreateItem>
<Copy DestinationFolder="$(destination)\%(Culture)"
SourceFiles="@(SatelliteAssemblies)" />
</Target>
<Target Name="CleanUp">
<Delete Files="@(Resources)"/>
</Target>
</Project>
The satellite assemblies seem to compile and embed correctly however when I place them with my application, they are not recognized and it defaults back to the default culture resources. If I build the project with Visual Studio and use the assemblies it creates with that, they load fine.
I must be missing something in causing the application to recognize my externally built assemblies. They are all named the same and the sizes are nearly the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 AL 目标中,您可以设置程序集中每个资源文件要使用的内部命名空间。设置正确的命名空间允许应用程序正确查找程序集中的资源。
In the AL target, you can set an internal namespace to be used by each resource file in the assembly. Setting the correct namespace allowed the application to correctly find the resources in the assemblies.