I'm using generic FunctionLoader from my original QA, which includes more code and usage, here https://stackoverflow.com/a/71514895/492624
I'd like to return function, that has the same signature, but internally measures the original delegate call.
This must be implemented/usable in net6.0
framework (Microsoft.NET.Sdk.Web sdk), and can be limited (as the FunctionLoader already is) to Windows platform.
so something like should be the wrapping delegate function
var sw = Stopwatch.StartNew();
var rtn = T();
sw.Stop();
Console.WriteLine($"elapsed: {sw.Elapsed}"
return rtn;
Snippet from original QA, which i'd like to modify:
public static T LoadFunction<T>(string dllPath, string functionName)
{
// normalize
if (!Path.IsPathFullyQualified(dllPath))
{
dllPath = Path.GetFullPath(dllPath);
}
// Get preloaded or load the library on-demand
IntPtr hModule = LoadedLibraries.GetOrAdd(
dllPath,
valueFactory: (string dllPath) =>
{
IntPtr loaded = LoadLibrary(dllPath);
if (loaded == IntPtr.Zero)
{
throw new DllNotFoundException($"Library not found in path {dllPath}");
}
return loaded;
}
);
// Load function
var functionAddress = GetProcAddress(hModule, functionName);
if (functionAddress == IntPtr.Zero)
{
throw new EntryPointNotFoundException($"Function {functionName} not found in {dllPath}");
}
var originalDelegate = (T)(object)(Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T)));
// Return wrapper, that has the same delegate signature (T) but internally measures and logs the elapsed, before returning the value to caller
return ???
}