Skip to content

Commit 6277467

Browse files
authored
Create NullCheckExtensions.cs
1 parent ab573a9 commit 6277467

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

NullCheckExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// **************************************************************** //
2+
//
3+
// Copyright (c) RimuruDev. All rights reserved.
4+
// Contact me:
5+
// - Gmail: rimuru.dev@gmail.com
6+
// - GitHub: https://github.com/RimuruDev
7+
// - LinkedIn: https://www.linkedin.com/in/rimuru/
8+
// - GitHub Organizations: https://github.com/Rimuru-Dev
9+
//
10+
// **************************************************************** //
11+
12+
using System;
13+
using System.Diagnostics;
14+
using Debug = UnityEngine.Debug;
15+
16+
namespace RimuruDev
17+
{
18+
public static class NullCheckExtensions
19+
{
20+
public static T IfNotNull<T>(this T obj, Action<T> action) where T : class
21+
{
22+
if (obj != null)
23+
{
24+
action?.Invoke(obj);
25+
}
26+
else
27+
{
28+
var stackTrace = new StackTrace();
29+
30+
var methodCall = stackTrace.GetFrame(1).GetMethod();
31+
32+
Debug.LogWarning($"Null reference at method: {methodCall.Name} in {methodCall.DeclaringType?.Name}, line: {new StackFrame(1, true).GetFileLineNumber()}");
33+
}
34+
35+
return obj;
36+
}
37+
}
38+
39+
}

0 commit comments

Comments
 (0)