2016-11-08 90 views
1

我即将制定简化翻译工具的解决方案。因此,我目前尝试从我的代码中自动编译一个Satellite Assembly。是否有可能从代码内生成卫星组件?

所以,我想才达到的替换下列命令手动运行:

AL.exe /culture:de /out:de\TestResource.resources.dll /embed:TestResource.de.resources

到目前为止,我已经测试生成一个.dll文件,它的工作。但嵌入/链接如下所示的资源没有任何影响,但扩大了dll的大小。所以显然它在那里,但不可用,就像生成的dll是一个Satellite Assembly。

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其中只包含本地化资源的一种语言,因此,它是可以作为一个卫星总装?

回答

1

最后我设法从代码生成卫星组件。

下面的代码生成一个适当的resourcefile:

// 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!!"); 
} 

使用此的resourcefile有一些compileroptions它们是必要的:

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 
); 
相关问题