Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Aug 20, 2023
1 parent 35a878a commit 2fd3ca4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions DotnetRuntimeBootstrapper.AppHost.Core/Native/NativeLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DotnetRuntimeBootstrapper.AppHost.Core.Native;

internal partial class NativeLibrary : NativeResource
{
private readonly Dictionary<string, Delegate> _functionTable = new(StringComparer.Ordinal);
private readonly Dictionary<string, Delegate> _functionsByName = new(StringComparer.Ordinal);

public NativeLibrary(nint handle)
: base(handle)
Expand All @@ -16,17 +16,17 @@ public NativeLibrary(nint handle)

public TDelegate GetFunction<TDelegate>(string functionName) where TDelegate : Delegate
{
if (_functionTable.TryGetValue(functionName, out var funcCached))
return (TDelegate)funcCached;
if (_functionsByName.TryGetValue(functionName, out var cached) && cached is TDelegate cachedCasted)
return cachedCasted;

var address = NativeMethods.GetProcAddress(Handle, functionName);
if (address == 0)
throw new Win32Exception();

var func = (TDelegate)Marshal.GetDelegateForFunctionPointer(address, typeof(TDelegate));
_functionTable[functionName] = func;
var function = (TDelegate)Marshal.GetDelegateForFunctionPointer(address, typeof(TDelegate));
_functionsByName[functionName] = function;

return func;
return function;
}

protected override void Dispose(bool disposing) =>
Expand Down

0 comments on commit 2fd3ca4

Please sign in to comment.