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
Fixup weird Array casting dead code in Min/MaxLengthAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Jun 19, 2017
1 parent 4721339 commit 6259768
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
<data name="MinLengthAttribute_ValidationError" xml:space="preserve">
<value>The field {0} must be a string or array type with a minimum length of '{1}'.</value>
</data>
<data name="LengthAttribute_InvalidValueType" xml:space="preserve">
<value>The field of type {0} must be a string, array or ICollection type.</value>
</data>
<data name="PhoneAttribute_Invalid" xml:space="preserve">
<value>The {0} field is not a valid phone number.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,19 @@ public override bool IsValid(object value)
{
return true;
}
var str = value as string;
if (str != null)
if (value is string str)
{
length = str.Length;
}
else
{
ICollection collection = value as ICollection;

if (collection != null)
if (value is ICollection collection)
{
length = collection.Count;
}
else
{
// A cast exception previously occurred if a non-{string|array} property was passed
// in so preserve this behavior if the value does not implement ICollection
length = ((Array)value).Length;
throw new InvalidCastException(SR.Format(SR.LengthAttribute_InvalidValueType, value.GetType()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,19 @@ public override bool IsValid(object value)
{
return true;
}
var str = value as string;
if (str != null)
if (value is string str)
{
length = str.Length;
}
else
{
ICollection collection = value as ICollection;

if (collection != null)
if (value is ICollection collection)
{
length = collection.Count;
}
else
{
// A cast exception previously occurred if a non-{string|array} property was passed
// in so preserve this behavior if the value does not implement ICollection
length = ((Array)value).Length;
throw new InvalidCastException(SR.Format(SR.LengthAttribute_InvalidValueType, value.GetType()));
}
}

Expand Down

0 comments on commit 6259768

Please sign in to comment.