Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ResXFileRef from this project #6

Merged
merged 11 commits into from
Dec 3, 2023
7 changes: 7 additions & 0 deletions System.Resources.NetStandard/ResXDataNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,13 @@ public Type GetType(string name, bool throwOnError, bool ignoreCase)
return result;
}

// hard-code the assembly name for ResXFileRef and replace it with our own type instead of loading the one from System.Windows.Forms
if (name == "System.Resources.ResXFileRef, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this string to ResXConstants?
It's nice to keep all the magic strings in one place.

Also, I noticed the example Resx I made with the VS editor didn't have this whole string as it's type

<data name="Test" type="System.Resources.ResXFileRef, System.Windows.Forms">
  <value>TestResources\Files\Test.xml;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>

Perhaps we can match on a StartsWith System.Resources.ResXFileRef, System.Windows.Forms (but still keep the constant in ResXConstants

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This also causes the namespace to change when we write the resx.

  1. We should probably make sure we can still read the data after we write the changed namespace
  2. This breaks compatibility with visual editors. Not sure what to do about that

{
result = typeof(ResXFileRef);
return result;
}

// Missed in cache, try to resolve the type from the reference assemblies.
if (name.IndexOf(',') != -1)
{
Expand Down
28 changes: 28 additions & 0 deletions Tests/ResxDataNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Drawing;
using System.Reflection;
using Xunit;

namespace System.Resources.NetStandard.Tests
Expand All @@ -18,5 +20,31 @@ public void ResxDataNode_ResXFileRefConstructor()
Assert.Equal(nodeName, dataNode.Name);
Assert.Same(fileRef, dataNode.FileRef);
}

[Fact]
public void ResxDataNode_CreateForResXFileRef()
{
// Simulate an XML file stored in resources, which uses a ResXFileRef. The actual resx XML looks like:
bgrainger marked this conversation as resolved.
Show resolved Hide resolved
/*
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="Test" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Test.xml;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
*/
var nodeInfo = new DataNodeInfo
{
Name = "Test",
ReaderPosition = new Point(1, 2),
TypeName = "System.Resources.ResXFileRef, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TypeName could also lean on the ResXConstants value once moved. Consolidates the special strings a bit.

ValueData = "TestResources/Files/Test.xml;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8",
};
var dataNode = new ResXDataNode(nodeInfo, null);
var typeResolver = new AssemblyNamesTypeResolutionService(Array.Empty<AssemblyName>());
Assert.Equal("Test", dataNode.Name);
Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Root>
<Element>Text</Element>
</Root>", dataNode.GetValue(typeResolver));
}
}
}
4 changes: 4 additions & 0 deletions Tests/TestResources/Files/Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Element>Text</Element>
</Root>
Loading