From e0f5d5b4fa88a54e45056dd07f0176e600ea5a80 Mon Sep 17 00:00:00 2001 From: desmondinho Date: Fri, 5 Jul 2024 22:47:14 -0500 Subject: [PATCH] refactor(components): utilize UnsafeAccessor to get JSRuntime in ElementReferenceExtensions --- src/LumexUI/Extensions/ElementReferenceExtensions.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/LumexUI/Extensions/ElementReferenceExtensions.cs b/src/LumexUI/Extensions/ElementReferenceExtensions.cs index 88d83cf3..fc897743 100644 --- a/src/LumexUI/Extensions/ElementReferenceExtensions.cs +++ b/src/LumexUI/Extensions/ElementReferenceExtensions.cs @@ -3,7 +3,7 @@ // See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE using System.Diagnostics.CodeAnalysis; -using System.Reflection; +using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; @@ -13,8 +13,8 @@ namespace LumexUI.Extensions; [ExcludeFromCodeCoverage] public static class ElementReferenceExtensions { - private static readonly PropertyInfo? _jsRuntimeProp = typeof( WebElementReferenceContext ) - .GetProperty( "JSRuntime", BindingFlags.Instance | BindingFlags.NonPublic ); + [UnsafeAccessor( UnsafeAccessorKind.Field, Name = "k__BackingField" )] + private static extern ref IJSRuntime GetJSRuntime( WebElementReferenceContext context ); public static ValueTask GetScrollHeightAsync( this ElementReference elementReference ) { @@ -29,7 +29,6 @@ private static IJSRuntime GetJSRuntime( this ElementReference elementReference ) throw new InvalidOperationException( "ElementReference has not been configured correctly." ); } - var jsRuntime = (IJSRuntime?)_jsRuntimeProp?.GetValue( context ); - return jsRuntime ?? throw new InvalidOperationException( "No JavaScript runtime found." ); + return GetJSRuntime( context ) ?? throw new InvalidOperationException( "No JavaScript runtime found." ); } }