CommunityToolkit / CommunityToolkit/dotnet
out parameter for Guard.IsAssignableToType<T>
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Overview
Add a ``Guard.IsAssignableToType`` with an out parameter of ``out T cast``
### API breakdown
```cs
///
/// Asserts that the input value can be assigned to a specified type.
///
/// The type to check the input value against.
/// The input to test.
/// The input as a .
/// The name of the input parameter being tested.
/// Thrown if can't be assigned to type .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsAssignableToType(object value, out T? cast, [CallerArgumentExpression("value")] string name = "")
{
if (value is T)
{
cast = (T)value;
return;
}
ThrowHelper.ThrowArgumentExceptionForIsAssignableToType(value, name);
}
```
### Usage example
```cs
public void Foo(IBase arg)
{
Guard.IsAssignableToType(arg, out Child child);
...
}
```
### Breaking change?
No
### Alternatives
```cs
public void Foo(IBase arg)
{
Guard.IsAssignableToType(arg);
var child = (Child)arg;
...
}
```
### Additional context
_No response_
### Help us help you
Yes, I'd like to be assigned to work on this item
Contributor guide
Assessment
This issue has not been assessed yet.