diff --git a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs
index 74fdf9090e6b..b9e4122fbc9e 100644
--- a/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs
+++ b/src/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs
@@ -1844,7 +1844,6 @@ public static class ProfileOptimization
}
namespace System.Runtime.CompilerServices
{
- [Serializable]
public sealed class SwitchExpressionException : System.InvalidOperationException
{
public SwitchExpressionException() { }
diff --git a/src/System.Runtime.Extensions/src/Resources/Strings.resx b/src/System.Runtime.Extensions/src/Resources/Strings.resx
index 20981ccb5fc1..274bdd74054b 100644
--- a/src/System.Runtime.Extensions/src/Resources/Strings.resx
+++ b/src/System.Runtime.Extensions/src/Resources/Strings.resx
@@ -295,6 +295,9 @@
Windows Principal functionality is not supported on this platform.
+
+ Unmatched value was {0}.
+
The path '{0}' is too long, or a component of the specified path is too long.
diff --git a/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs b/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs
index f227cf3cfd4b..950b283b355d 100644
--- a/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs
+++ b/src/System.Runtime.Extensions/src/System/Runtime/CompilerServices/SwitchExpressionException.cs
@@ -13,29 +13,53 @@ namespace System.Runtime.CompilerServices
///
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
- public sealed class SwitchExpressionException : InvalidOperationException//, ISerializable
+ public sealed class SwitchExpressionException : InvalidOperationException
{
public SwitchExpressionException()
: base(SR.Arg_SwitchExpressionException) { }
public SwitchExpressionException(object unmatchedValue)
- : base()
+ : this()
{
UnmatchedValue = unmatchedValue;
}
- private SwitchExpressionException(SerializationInfo info, StreamingContext context)
+ private SwitchExpressionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
- info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object));
+ if (info == null)
+ {
+ throw new ArgumentNullException(nameof(info));
+ }
+ UnmatchedValue = info.GetValue(nameof(UnmatchedValue), typeof(object));
}
public object UnmatchedValue { get; }
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
- base.GetObjectData(info, context);
+ base.GetObjectData(info, context);
+ if (info == null)
+ {
+ throw new ArgumentNullException(nameof(info));
+ }
info.AddValue(nameof(UnmatchedValue), UnmatchedValue, typeof(object));
}
+
+ public override string Message
+ {
+ get
+ {
+ string s = base.Message;
+ if (UnmatchedValue != null)
+ {
+ string valueMessage = SR.Format(SR.SwitchExpressionException_UnmatchedValue, UnmatchedValue?.ToString());
+ if (s == null)
+ return valueMessage;
+ return s + Environment.NewLine + valueMessage;
+ }
+ return s;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs b/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs
index dff1c4cd982d..5bc035d5c931 100644
--- a/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs
+++ b/src/System.Runtime.Extensions/tests/System/Runtime/CompilerServices/SwitchExpressionExceptionTests.cs
@@ -24,7 +24,7 @@ public static void Constructor_StringAsObjectArg()
var ex = new SwitchExpressionException(message);
Assert.NotEqual(message, ex.Message);
- Assert.Equal(message, ex.UnmatchedValue);
+ Assert.Same(message, ex.UnmatchedValue);
}
[Fact]
@@ -32,16 +32,23 @@ public void UnmatchedValue()
{
var ex = new SwitchExpressionException(34);
Assert.Equal(34, ex.UnmatchedValue);
+ Assert.Contains(ex.UnmatchedValue.ToString(), ex.Message);
var data = new byte[] { 1, 2, 3 };
ex = new SwitchExpressionException(data);
- Assert.Equal(data, ex.UnmatchedValue);
+ Assert.Same(data, ex.UnmatchedValue);
+ Assert.Contains(ex.UnmatchedValue.ToString(), ex.Message);
ex = new SwitchExpressionException(true);
Assert.Equal(true, ex.UnmatchedValue);
+ Assert.Contains(ex.UnmatchedValue.ToString(), ex.Message);
ex = new SwitchExpressionException("34");
- Assert.Equal("34", ex.UnmatchedValue);
+ Assert.Same("34", ex.UnmatchedValue);
+ Assert.Contains(ex.UnmatchedValue.ToString(), ex.Message);
+
+ ex = new SwitchExpressionException(null);
+ Assert.Null(ex.UnmatchedValue);
}
}
}