Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Apply PR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan committed Jan 31, 2019
1 parent 3ec5b78 commit 45556db
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,6 @@ public static class ProfileOptimization
}
namespace System.Runtime.CompilerServices
{
[Serializable]
public sealed class SwitchExpressionException : System.InvalidOperationException
{
public SwitchExpressionException() { }
Expand Down
3 changes: 3 additions & 0 deletions src/System.Runtime.Extensions/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@
<data name="PlatformNotSupported_Principal" xml:space="preserve">
<value>Windows Principal functionality is not supported on this platform.</value>
</data>
<data name="SwitchExpressionException_UnmatchedValue" xml:space="preserve">
<value>Unmatched value was {0}.</value>
</data>
<data name="IO_PathTooLong_Path" xml:space="preserve">
<value>The path '{0}' is too long, or a component of the specified path is too long.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,53 @@ namespace System.Runtime.CompilerServices
/// </summary>
[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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ 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]
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);
}
}
}

0 comments on commit 45556db

Please sign in to comment.