/
DotNetObjectReferenceOfT.cs
102 lines (89 loc) · 2.65 KB
/
DotNetObjectReferenceOfT.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.JSInterop.Infrastructure;
using static Microsoft.AspNetCore.Internal.LinkerFlags;
namespace Microsoft.JSInterop;
/// <summary>
/// Wraps a JS interop argument, indicating that the value should not be serialized as JSON
/// but instead should be passed as a reference.
///
/// To avoid leaking memory, the reference must later be disposed by JS code or by .NET code.
/// </summary>
/// <typeparam name="TValue">The type of the value to wrap.</typeparam>
public sealed class DotNetObjectReference<[DynamicallyAccessedMembers(JSInvokable)] TValue> :
IDotNetObjectReference, IDisposable where TValue : class
{
private readonly TValue _value;
private long _objectId;
private JSRuntime? _jsRuntime;
/// <summary>
/// Initializes a new instance of <see cref="DotNetObjectReference{TValue}" />.
/// </summary>
/// <param name="value">The value to pass by reference.</param>
internal DotNetObjectReference(TValue value)
{
_value = value;
}
/// <summary>
/// Gets the object instance represented by this wrapper.
/// </summary>
public TValue Value
{
get
{
ThrowIfDisposed();
return _value;
}
}
internal long ObjectId
{
get
{
ThrowIfDisposed();
Debug.Assert(_objectId != 0, "Accessing ObjectId without tracking is always incorrect.");
return _objectId;
}
set
{
ThrowIfDisposed();
_objectId = value;
}
}
internal JSRuntime? JSRuntime
{
get
{
ThrowIfDisposed();
return _jsRuntime;
}
set
{
ThrowIfDisposed();
_jsRuntime = value;
}
}
object IDotNetObjectReference.Value => Value;
internal bool Disposed { get; private set; }
/// <summary>
/// Stops tracking this object reference, allowing it to be garbage collected
/// (if there are no other references to it). Once the instance is disposed, it
/// can no longer be used in interop calls from JavaScript code.
/// </summary>
public void Dispose()
{
if (!Disposed)
{
Disposed = true;
if (_jsRuntime != null)
{
_jsRuntime.ReleaseObjectReference(_objectId);
}
}
}
internal void ThrowIfDisposed()
{
ObjectDisposedException.ThrowIf(Disposed, this);
}
}