caller.cs
caller.cs
is a
C# program whose main purposes are
- to provide a delegate (
funcPtrType
)
- to store a reference to function that matches the delegate's type (
funcPtr
) and
- to call the function to which that reference points to (
callFuncPtr
).
using System;
namespace TQ84 {
public class caller {
public delegate float funcPtrType(float a, float b);
public funcPtrType funcPtr;
public void callFuncPtr(float a, float b) {
float val = funcPtr(a,b);
Console.WriteLine($"I have called the function pointer with {a:f} and {b:f} and it returned {val:f}");
}
}
}
Create PowerShell class with callback method
class test {
static [float] add([float] $a, [float] $b) { return $a + $b}
static [float] sub([float] $a, [float] $b) { return $a - $b}
static [float] mul([float] $a, [float] $b) { return $a * $b}
static [TQ84.caller+funcPtrType] createDelegate($methodName) {
$delegate = [System.Delegate]::CreateDelegate([TQ84.caller+funcPtrType], [test], $methodName, $true, $true)
return $delegate
}
}
$callAdd = [TQ84.caller]::new(); $callAdd.funcPtr = [test]::createDelegate('add')
$callSub = [TQ84.caller]::new(); $callSub.funcPtr = [test]::createDelegate('sub')
$callMul = [TQ84.caller]::new(); $callMul.funcPtr = [test]::createDelegate('mul')
$callAdd.callFuncPtr(19, 23)
$callSub.callFuncPtr(55, 13)
$callMul.callFuncPtr( 6, 7)