Skip to content

Conversation

@SadPencil
Copy link
Contributor

@SadPencil SadPencil commented Sep 25, 2025

@StephenMolloy

This duplicate definition was added in 04988ec. It has already been added in bbfaee3.

Surprisingly, the corresponding PR #119865 does not introduce this duplication. It seems that when merging the PR, the corresponding branch was not up-to-date with main branch, causing such a duplication.

internal sealed class XmlSerializerAppContextSwitchScope : IDisposable
{
private readonly string _name;
private readonly string _cachedName;
private readonly bool _hadValue;
private readonly bool _originalValue;
public bool OriginalValue => _originalValue;
public bool CurrentValue => AppContext.TryGetSwitch(_name, out bool value) && value;
public XmlSerializerAppContextSwitchScope(string name, bool value, string cachedName = null)
{
_name = name;
_cachedName = cachedName ?? GuessCachedName(name);
_hadValue = AppContext.TryGetSwitch(name, out _originalValue);
AppContext.SetSwitch(name, value);
ClearCachedSwitch(_cachedName);
}
public void Dispose()
{
if (_hadValue)
AppContext.SetSwitch(_name, _originalValue);
else
// There's no "unset", so pick a default or false
AppContext.SetSwitch(_name, false);
ClearCachedSwitch(_cachedName);
}
private static void ClearCachedSwitch(string name)
{
Type t = Type.GetType("System.Xml.LocalAppContextSwitches, System.Private.Xml");
Assert.NotNull(t);
FieldInfo fi = t.GetField(name, BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(fi);
fi.SetValue(null, 0);
}
private static string GuessCachedName(string name)
{
// Switch names are typically of the form "Switch.System.Xml.SomeFeature"
// The cached field is typically "s_someFeature"
int idx = name.LastIndexOf('.');
if (idx > 0 && idx < name.Length - 1)
{
string feature = name.Substring(idx + 1);
return "s_" + char.ToLowerInvariant(feature[0]) + feature.Substring(1);
}
throw new ArgumentException($"Cannot guess cached field name from switch name '{name}'");
}
}

internal sealed class XmlSerializerAppContextSwitchScope : IDisposable
{
private readonly string _name;
private readonly string _cachedName;
private readonly bool _hadValue;
private readonly bool _originalValue;
public bool OriginalValue => _originalValue;
public bool CurrentValue => AppContext.TryGetSwitch(_name, out bool value) && value;
public XmlSerializerAppContextSwitchScope(string name, bool value, string cachedName = null)
{
_name = name;
_cachedName = cachedName ?? GuessCachedName(name);
_hadValue = AppContext.TryGetSwitch(name, out _originalValue);
AppContext.SetSwitch(name, value);
ClearCachedSwitch(_cachedName);
}
public void Dispose()
{
if (_hadValue)
AppContext.SetSwitch(_name, _originalValue);
else
// There's no "unset", so pick a default or false
AppContext.SetSwitch(_name, false);
ClearCachedSwitch(_cachedName);
}
private static void ClearCachedSwitch(string name)
{
Type t = Type.GetType("System.Xml.LocalAppContextSwitches, System.Private.Xml");
Assert.NotNull(t);
FieldInfo fi = t.GetField(name, BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(fi);
fi.SetValue(null, 0);
}
private static string GuessCachedName(string name)
{
// Switch names are typically of the form "Switch.System.Xml.SomeFeature"
// The cached field is typically "s_someFeature"
int idx = name.LastIndexOf('.');
if (idx > 0 && idx < name.Length - 1)
{
string feature = name.Substring(idx + 1);
return "s_" + char.ToLowerInvariant(feature[0]) + feature.Substring(1);
}
throw new ArgumentException($"Cannot guess cached field name from switch name '{name}'");
}

These two XmlSerializerAppContextSwitchScope definitions are identical.

It causes build failures shown below.

src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs(3105,12): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'XmlSerializerAppContextSwitchScope' already defines a member called 'XmlSerializerAppContextSwitchScope' with the same parameter types

Copilot AI review requested due to automatic review settings September 25, 2025 02:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes a duplicate class definition that was causing build failures. The XmlSerializerAppContextSwitchScope class was accidentally duplicated in the test file, resulting in a compiler error due to multiple definitions with the same constructor signature.

  • Removes the duplicate XmlSerializerAppContextSwitchScope class definition from the test file
  • Fixes the CS0111 compilation error about duplicate member definitions
  • Cleans up code duplication introduced in a previous commit

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 25, 2025
@SadPencil SadPencil changed the title Remove duplicate XmlSerializerAppContextSwitchScope added by Copilot Remove duplicate XmlSerializerAppContextSwitchScope definition Sep 25, 2025
Copy link
Member

@StephenMolloy StephenMolloy left a comment

Choose a reason for hiding this comment

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

:shipit:

Looks like this odd duplication only happened in 'main'. 'release/10.0' and 'release/10.0-rc2' are both fine. Weird.

@StephenMolloy StephenMolloy merged commit 3a353f8 into dotnet:main Sep 25, 2025
90 checks passed
@SadPencil SadPencil deleted the remove-duplicate-type branch September 25, 2025 05:44
@github-actions github-actions bot locked and limited conversation to collaborators Oct 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Serialization community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants