Browse Source

remove some duplicate casts

pull/9908/head
Simon Cropp 4 years ago
parent
commit
c89f221f6d
  1. 21
      src/Avalonia.Base/Controls/NameScopeExtensions.cs

21
src/Avalonia.Base/Controls/NameScopeExtensions.cs

@ -25,13 +25,18 @@ namespace Avalonia.Controls
var result = nameScope.Find(name); var result = nameScope.Find(name);
if (result != null && !(result is T)) if (result == null)
{ {
throw new InvalidOperationException( return (T?)result;
$"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'."); }
if (result is T typed)
{
return typed;
} }
return (T?)result; throw new InvalidOperationException(
$"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.");
} }
/// <summary> /// <summary>
@ -74,13 +79,13 @@ namespace Avalonia.Controls
throw new KeyNotFoundException($"Could not find control '{name}'."); throw new KeyNotFoundException($"Could not find control '{name}'.");
} }
if (!(result is T)) if (result is T typed)
{ {
throw new InvalidOperationException( return typed;
$"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.");
} }
return (T)result; throw new InvalidOperationException(
$"Expected control '{name}' to be '{typeof(T)} but it was '{result.GetType()}'.");
} }
/// <summary> /// <summary>

Loading…
Cancel
Save