好吧,我找到了一个可能的解决方案:
我创建了一个通用 Invoke 方法,该方法创建并缓存所有委托以供将来使用
public void Select(uint target)
{
fixed (void* pThis = &this)
{
Generic.Invoke<Action<uint, uint>>(this.VTable[0xC0], CallingConvention.ThisCall)
((uint)pThis, target);
}
}
[FieldOffset(0x00)]
public uint* VTable;
缓存:
public static T Invoke<T>(uint addr, CallingConvention conv) where T : class
{
var type = typeof(T);
if (!cache.Contains(type))
cache.Set<T>(type, NativeHelper.GetDelegateForFunctionPointer<T>(addr, conv));
return cache.Get<T>(type);
}
以及创建函数的函数(适用于通用 Func/Action)
public static T GetDelegateForFunctionPointer<T>(uint ptr, CallingConvention conv)
where T : class
{
var delegateType = typeof(T);
var method = delegateType.GetMethod("Invoke");
var returnType = method.ReturnType;
var paramTypes =
method
.GetParameters()
.Select((x) => x.ParameterType)
.ToArray();
var invoke = new DynamicMethod("Invoke", returnType, paramTypes, typeof(Delegate));
var il = invoke.GetILGenerator();
for (int i = 0; i < paramTypes.Length; i++)
il.Emit(OpCodes.Ldarg, i);
il.Emit(OpCodes.Ldc_I4, ptr);
il.EmitCalli(OpCodes.Calli, conv, returnType, paramTypes);
il.Emit(OpCodes.Ret);
return invoke.CreateDelegate(delegateType) as T;
}