Allow configuring ResX assembly names

Resource export sometimes needs framework-specific ResX type names. Exposing the WinForms and mscorlib assembly suffixes keeps the writer default-compatible while letting callers choose older resource type names when needed.

Assisted-by: OpenCode:openai/gpt-5.5:OpenCode
This commit is contained in:
Siegfried Pammer
2026-10-08 19:28:07 +02:00
parent 6b43671564
commit 73ec67e2af
2 changed files with 46 additions and 4 deletions
@@ -65,6 +65,25 @@ namespace ICSharpCode.Decompiler.Tests.Util
}
}
[Test]
public void ResXResourceWriter_UsesConfiguredAssemblyNames()
{
using var stream = new MemoryStream();
using (var writer = new ResXResourceWriter(stream))
{
writer.WinFormsAssemblyName = ", System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
writer.MSCorLibAssemblyName = ", mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
writer.AddResource("number", 1);
writer.AddResource("missing", (object?)null);
}
string resx = Encoding.UTF8.GetString(stream.ToArray());
Assert.That(resx, Does.Contain("System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0"));
Assert.That(resx, Does.Contain("System.Int32, mscorlib, Version=2.0.0.0"));
Assert.That(resx, Does.Contain("System.Resources.ResXNullRef, System.Windows.Forms, Version=2.0.0.0"));
Assert.That(resx, Does.Not.Contain("Version=4.0.0.0"));
}
[Test]
public void WellFormedFile_ReadsAllEntryKinds()
{
@@ -98,9 +98,32 @@ namespace ICSharpCode.Decompiler.Util
Dispose(false);
}
const string WinFormsAssemblyName = ", System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
const string MSCorLibAssemblyName = ", mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
const string ResXNullRefTypeName = "System.Resources.ResXNullRef" + WinFormsAssemblyName;
const string DefaultWinFormsAssemblyName = ", System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
const string DefaultMSCorLibAssemblyName = ", mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
string winFormsAssemblyName = DefaultWinFormsAssemblyName;
string msCorLibAssemblyName = DefaultMSCorLibAssemblyName;
public string WinFormsAssemblyName {
get => winFormsAssemblyName;
set {
ThrowIfWritingStarted();
winFormsAssemblyName = value ?? throw new ArgumentNullException(nameof(value));
}
}
public string MSCorLibAssemblyName {
get => msCorLibAssemblyName;
set {
ThrowIfWritingStarted();
msCorLibAssemblyName = value ?? throw new ArgumentNullException(nameof(value));
}
}
void ThrowIfWritingStarted()
{
if (writer != null || written)
throw new InvalidOperationException("Assembly names cannot be changed after writing started.");
}
void InitWriter()
{
@@ -222,7 +245,7 @@ namespace ICSharpCode.Decompiler.Util
{
case null:
// nulls written as ResXNullRef
WriteString(name, "", ResXNullRefTypeName, comment);
WriteString(name, "", "System.Resources.ResXNullRef" + WinFormsAssemblyName, comment);
break;
case string s:
WriteString(name, s, null, comment);