public static async Task GenerateFile(ResXGeneratorOptions options, ResXGeneratorResult result) { var inputFilePath = options.InputFilePath; var targetsPcl2Framework = options.TargetPcl2Framework; var outputFilePath = !string.IsNullOrEmpty(options.OutputFilePath) ? options.OutputFilePath : $"{Path.GetFileNameWithoutExtension(inputFilePath)}.Designer.cs"; //no need to escape/cleanup, StronglyTypedResourceBuilder does that var className = !string.IsNullOrEmpty(options.ResultClassName) ? options.ResultClassName : Path.GetFileNameWithoutExtension(inputFilePath); var provider = new CSharpCodeProvider(); var resourceList = new Dictionary <object, object>(); await Task.Run(() => { if (className == null) { result.Errors.Add(new CompilerError(inputFilePath, 0, 0, null, "Class name cannot be null")); return; } using (var r = new ResXResourceReader(inputFilePath)) { r.BasePath = Path.GetDirectoryName(inputFilePath); foreach (DictionaryEntry e in r) { resourceList.Add(e.Key, e.Value); } } string[] unmatchable; var ccu = StronglyTypedResourceBuilder.Create(resourceList, className, options.ResultNamespace, provider, options.GenerateInternalClass, out unmatchable); if (targetsPcl2Framework) { FixupPclTypeInfo(ccu, result); } foreach (var p in unmatchable) { var msg = $"Could not generate property for resource ID '{p}'"; result.Errors.Add(new CompilerError(inputFilePath, 0, 0, null, msg)); } // Avoid race if ResXFileCodeGenerator is called more than once for the same file using (var fw = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { using (var w = new StreamWriter(fw, Encoding.UTF8)) provider.GenerateCodeFromCompileUnit(ccu, w, new CodeGeneratorOptions()); } result.GeneratedFilePath = outputFilePath; }); }
//works with .NET 4.5.1 and Mono 3.4.0 static void FixupPclTypeInfo(CodeCompileUnit ccu, ResXGeneratorResult result) { try { CodeObjectCreateExpression initExpr = GetInitExpr(ccu); var typeofExpr = (CodePropertyReferenceExpression)initExpr.Parameters[1]; typeofExpr.TargetObject = new CodeMethodInvokeExpression(typeofExpr.TargetObject, "GetTypeInfo"); } catch (Exception ex) { result.UnhandledException = ex; } }