-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize TypeName Parser API in ResXSerializationBinder (#11312)
* Utilize TypeName Parser API * clean up * address feedback
- Loading branch information
Showing
2 changed files
with
139 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/System.Windows.Forms/tests/UnitTests/System/Resources/ResXSerializationBinderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System.ComponentModel.Design; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Reflection.Metadata; | ||
|
||
namespace System.Resources.Tests; | ||
|
||
public class ResXSerializationBinderTests | ||
{ | ||
[Fact] | ||
public void ResXSerializationBinder_BindToType_FullyQualifiedName() | ||
{ | ||
TrackGetTypePathTypeResolutionService typeResolutionService = new(); | ||
ResXSerializationBinder binder = new(typeResolutionService); | ||
binder.BindToType(typeof(Button).Assembly.FullName!, typeof(Button).FullName!).Should().Be(typeof(Button)); | ||
typeResolutionService.FullyQualifiedAssemblyNamePath.Should().BeTrue(); | ||
typeResolutionService.FullyQualifiedAssemblyNameNoVersionPath.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ResXSerializationBinder_BindToType_FullName() | ||
{ | ||
TrackGetTypePathTypeResolutionService typeResolutionService = new(); | ||
ResXSerializationBinder binder = new(typeResolutionService); | ||
binder.BindToType(typeof(MyClass).Assembly.FullName!, typeof(MyClass).FullName!).Should().Be(typeof(MyClass)); | ||
typeResolutionService.FullNamePath.Should().BeTrue(); | ||
typeResolutionService.FullyQualifiedAssemblyNameNoVersionPath.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ResXSerializationBinder_BindToType_FullyQualifiedNameNoVersion() | ||
{ | ||
TrackGetTypePathTypeResolutionService typeResolutionService = new(); | ||
ResXSerializationBinder binder = new(typeResolutionService); | ||
binder.BindToType(typeof(Form).Assembly.FullName!, typeof(Form).FullName!).Should().Be(typeof(Form)); | ||
typeResolutionService.FullyQualifiedAssemblyNameNoVersionPath.Should().BeTrue(); | ||
typeResolutionService.FullNamePath.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ResXSerializationBinder_BindToName_TypeNameConverter_NoNameChange() | ||
{ | ||
ResXSerializationBinder binder = new(type => type?.AssemblyQualifiedName ?? string.Empty); | ||
binder.BindToName(typeof(Button), out string? assemblyName, out string? typeName); | ||
assemblyName.Should().Be(typeof(Button).Assembly.FullName); | ||
typeName.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ResXSerializationBinder_BindToName_TypeNameConverter_NameChange() | ||
{ | ||
string testAssemblyName = "TestAssembly"; | ||
string testTypeName = "TestType"; | ||
ResXSerializationBinder binder = new(type => $"{testTypeName}, {testAssemblyName}"); | ||
binder.BindToName(typeof(Button), out string? assemblyName, out string? typeName); | ||
assemblyName.Should().Be(testAssemblyName); | ||
typeName.Should().Be(typeName); | ||
} | ||
|
||
private class TrackGetTypePathTypeResolutionService : ITypeResolutionService | ||
{ | ||
public bool FullNamePath { get; private set; } | ||
public bool FullyQualifiedAssemblyNamePath { get; private set; } | ||
public bool FullyQualifiedAssemblyNameNoVersionPath { get; private set; } | ||
|
||
public Assembly? GetAssembly(AssemblyName name) => throw new NotImplementedException(); | ||
public Assembly? GetAssembly(AssemblyName name, bool throwOnError) => throw new NotImplementedException(); | ||
public string? GetPathOfAssembly(AssemblyName name) => throw new NotImplementedException(); | ||
public Type? GetType(string typeName) | ||
{ | ||
if (typeName == $"{typeof(Button).FullName}, {typeof(Button).Assembly.FullName}") | ||
{ | ||
FullyQualifiedAssemblyNamePath = true; | ||
return typeof(Button); | ||
} | ||
else if (typeName == typeof(MyClass).FullName) | ||
{ | ||
FullNamePath = true; | ||
return typeof(MyClass); | ||
} | ||
else | ||
{ | ||
TypeName parsed = TypeName.Parse($"{typeof(Form).FullName}, {typeof(Form).Assembly.FullName}"); | ||
AssemblyNameInfo? assemblyNameInfo = parsed.AssemblyName; | ||
string formNoVersionFullyQualifiedName = $"{typeof(Form).FullName}, {new AssemblyNameInfo( | ||
assemblyNameInfo!.Name, | ||
version: null, | ||
assemblyNameInfo.CultureName, | ||
assemblyNameInfo.Flags, | ||
assemblyNameInfo.PublicKeyOrToken).FullName}"; | ||
if (typeName == formNoVersionFullyQualifiedName) | ||
{ | ||
FullyQualifiedAssemblyNameNoVersionPath = true; | ||
return typeof(Form); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
public Type? GetType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] string name, bool throwOnError) => throw new NotImplementedException(); | ||
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
public Type? GetType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] string name, bool throwOnError, bool ignoreCase) => throw new NotImplementedException(); | ||
public void ReferenceAssembly(AssemblyName name) => throw new NotImplementedException(); | ||
} | ||
|
||
private class MyClass { } | ||
} |