C#C
C#3y ago
Tijmen

❔ TargetParameterCountException when trying to create a delegate from a method

I am trying to call the following private extern method via reflection:

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void ComputeDetailInstanceTransforms_Injected(IntPtr _unity_self, int patchX, int patchY, int layer, float density, out Bounds bounds, out BlittableArrayWrapper ret);

The out parameters make this slightly more difficult, and the last BlittableArrayWrapper is also a ref struct.

So here i go, i find the method and try to create the delegate and call it:

delegate void ComputeDetailInstanceTransforms_Injected(IntPtr _unity_self, int patchX, int patchY, int layer, float density, out Bounds bounds, out BlittableArrayWrapper ret);

var method = typeof(TerrainData).GetMethod("ComputeDetailInstanceTransforms_Injected", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

var d = method.CreateDelegate(typeof(ComputeDetailInstanceTransforms_Injected), terrainData) as ComputeDetailInstanceTransforms_Injected;

However upon creating the delegate i get a TargetParameterCountException, though i'm very confident i have the same parameter counts.

TargetParameterCountException: Parameter count mismatch.
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <9e5e67854f784ff09fdd9e0936e796e5>:0)
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at <9e5e67854f784ff09fdd9e0936e796e5>:0)
System.Reflection.RuntimeMethodInfo.CreateDelegate (System.Type delegateType, System.Object target) (at <9e5e67854f784ff09fdd9e0936e796e5>:0)
image.png
Was this page helpful?