forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaObjectExtensions.Android.cs
More file actions
45 lines (44 loc) · 1.27 KB
/
Copy pathJavaObjectExtensions.Android.cs
File metadata and controls
45 lines (44 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Runtime.CompilerServices;
using System.Text;
using Android.Runtime;
using System;
using Uno.Logging;
using Microsoft.Extensions.Logging;
namespace Uno.Extensions
{
public static class JavaObjectExtensions
{
/// <summary>
/// Runs the specified action if the native Java instance of the <paramref name="instance"/> is still available.
/// </summary>
/// <typeparam name="T">An <see cref="IJavaObject"/> instance.</typeparam>
/// <param name="instance">The .NET instance to check</param>
/// <param name="action">The action to execute if both the .NET instance and Java instance are available.</param>
public static void RunIfNativeInstanceAvailable<T>(
this T instance,
Action<T> action,
[CallerMemberName]string member = null,
[CallerLineNumber]int line = 0,
[CallerFilePath]string filePath = null
) where T : IJavaObject
{
if (instance.Handle != IntPtr.Zero)
{
action(instance);
}
else
{
if (instance.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
{
instance.Log().WarnFormat(
"Native invocation discarded for {0} at {1}:{2} ({3}). The object may not have been disposed properly by its owner."
, instance.GetType()
, member
, line
, filePath
);
}
}
}
}
}