是否可以从代码中生成附属程序集?
Is it possible to generate Satellite Assemblies from within code?
我将详细阐述一个简化翻译工具的解决方案。因此,我目前尝试从我的代码中自动编译卫星程序集。
所以我想要实现的是替换以下命令的手册运行:
AL.exe /culture:de /out:de\TestResource.resources.dll /embed:TestResource.de.resources
到目前为止,我已经测试了生成 .dll 文件的效果。但是 embedding/linking 如下所示的资源没有任何效果,但扩大了 dll 的大小。很明显它在那里但不能使用,就好像生成的 dll 是一个卫星程序集一样。
static void Main(string[] args)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "./output/satellite_test.dll";
parameters.EmbeddedResources.Add(@"./TestResource.en.resources");
parameters.LinkedResources.Add(@"./TestResource.de.resources");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, "");
}
有没有办法以编程方式生成只包含一种语言的本地化资源的 dll,以便它可以用作卫星程序集?
我终于成功地从代码中生成了卫星程序集。
以下代码生成适当的资源文件:
// Already the resourcefilename has to match the
// exact namespacepath of the original resourcename.
var resourcefileName = @"TranslationTest.Resources.TestResource.de.resources";
// File has to be a .resource file. (ResourceWriter instead of ResXResourceWriter)
// .resx not working and has to be converted into .resource file.
using (var resourceWriter = new ResourceWriter(resourcefileName))
{
resourceWriter.AddResource("testtext", "Language is german!!");
}
使用这个资源文件有一些必要的编译器选项:
CompilerParameters parameters = new CompilerParameters();
// Newly created assembly has to be a dll.
parameters.GenerateExecutable = false;
// Filename has to be like the original resourcename. Renaming afterwards does not work.
parameters.OutputAssembly = "./de/TranslationTest.resources.dll";
// Resourcefile has to be embedded in the new assembly.
parameters.EmbeddedResources.Add(resourcefileName);
最后编译程序集有一些必需的代码必须编译成:
// Culture information has to be part of the newly created assembly.
var assemblyAttributesAsCode = @"
using System.Reflection;
[assembly: AssemblyCulture(""de"")]";
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerResults results = codeProvider.CompileAssemblyFromSource(
parameters,
assemblyAttributesAsCode
);
我将详细阐述一个简化翻译工具的解决方案。因此,我目前尝试从我的代码中自动编译卫星程序集。
所以我想要实现的是替换以下命令的手册运行:
AL.exe /culture:de /out:de\TestResource.resources.dll /embed:TestResource.de.resources
到目前为止,我已经测试了生成 .dll 文件的效果。但是 embedding/linking 如下所示的资源没有任何效果,但扩大了 dll 的大小。很明显它在那里但不能使用,就好像生成的 dll 是一个卫星程序集一样。
static void Main(string[] args)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "./output/satellite_test.dll";
parameters.EmbeddedResources.Add(@"./TestResource.en.resources");
parameters.LinkedResources.Add(@"./TestResource.de.resources");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, "");
}
有没有办法以编程方式生成只包含一种语言的本地化资源的 dll,以便它可以用作卫星程序集?
我终于成功地从代码中生成了卫星程序集。
以下代码生成适当的资源文件:
// Already the resourcefilename has to match the
// exact namespacepath of the original resourcename.
var resourcefileName = @"TranslationTest.Resources.TestResource.de.resources";
// File has to be a .resource file. (ResourceWriter instead of ResXResourceWriter)
// .resx not working and has to be converted into .resource file.
using (var resourceWriter = new ResourceWriter(resourcefileName))
{
resourceWriter.AddResource("testtext", "Language is german!!");
}
使用这个资源文件有一些必要的编译器选项:
CompilerParameters parameters = new CompilerParameters();
// Newly created assembly has to be a dll.
parameters.GenerateExecutable = false;
// Filename has to be like the original resourcename. Renaming afterwards does not work.
parameters.OutputAssembly = "./de/TranslationTest.resources.dll";
// Resourcefile has to be embedded in the new assembly.
parameters.EmbeddedResources.Add(resourcefileName);
最后编译程序集有一些必需的代码必须编译成:
// Culture information has to be part of the newly created assembly.
var assemblyAttributesAsCode = @"
using System.Reflection;
[assembly: AssemblyCulture(""de"")]";
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerResults results = codeProvider.CompileAssemblyFromSource(
parameters,
assemblyAttributesAsCode
);