2016-11-08 86 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 
); 
相關問題