Skip to content

Commit 9f7f70a

Browse files
committed
Adds way to manually initialize a single delegate
1 parent 133f701 commit 9f7f70a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/NativeLibraryUtilities/NativeDelegateInitializer.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using System.Runtime.InteropServices;
34
using System.Linq;
45

@@ -9,6 +10,31 @@ namespace NativeLibraryUtilities
910
/// </summary>
1011
public static class NativeDelegateInitializer
1112
{
13+
#if NETSTANDARD
14+
/// <summary>
15+
/// Loads and setups a native delegate.
16+
/// </summary>
17+
/// <param name="library">The library to load from</param>
18+
/// <param name="nativeName">The native function name to load</param>
19+
/// <returns>A delegate that will call the native function</returns>
20+
public static T SetupNativeDelegate<T>(ILibraryInformation library, string nativeName)
21+
{
22+
return Marshal.GetDelegateForFunctionPointer<T>(library.LibraryLoader.GetProcAddress(nativeName));
23+
}
24+
#else
25+
/// <summary>
26+
/// Loads and setups a native delegate.
27+
/// </summary>
28+
/// <param name="library">The library to load from</param>
29+
/// <param name="nativeName">The native function name to load</param>
30+
/// <param name="delegateType">The type of delegate to create</param>
31+
/// <returns>A delegate that will call the native function</returns>
32+
public static Delegate SetupNativeDelegate(ILibraryInformation library, string nativeName, Type delegateType)
33+
{
34+
return Marshal.GetDelegateForFunctionPointer(library.LibraryLoader.GetProcAddress(nativeName), delegateType);
35+
}
36+
#endif
37+
1238
/// <summary>
1339
/// Sets up all native delegate in the type passed as the generic parameter
1440
/// </summary>

0 commit comments

Comments
 (0)