Skip to content

Commit a56141b

Browse files
committed
Updated SolidUtilities
1 parent b4370b8 commit a56141b

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace SolidUtilities.Extensions
2+
{
3+
using System;
4+
5+
/// <summary> Different useful extensions for <see cref="object"/>.</summary>
6+
internal static class ObjectExtensions
7+
{
8+
/// <summary>
9+
/// Casts <paramref name="obj"/> to <typeparamref name="T"/> or returns a default instance of
10+
/// <typeparamref name="T"/> if the cast fails.
11+
/// </summary>
12+
/// <param name="obj">The object to cast.</param>
13+
/// <typeparam name="T">The type to cast to.</typeparam>
14+
/// <returns>The cast object of type <typeparamref name="T"/> or default instance.</returns>
15+
public static T CastOrDefault<T>(this object obj)
16+
{
17+
try
18+
{
19+
return (T) obj;
20+
}
21+
catch (InvalidCastException)
22+
{
23+
return default(T);
24+
}
25+
}
26+
}
27+
}

Dependencies/SolidUtilities/Runtime/Extensions/ObjectExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)