C
C#13mo 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);
[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;
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)
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)
No description
3 Replies
Tijmen
TijmenOP13mo ago
Here with the debugger attached, In the bottom right you see the list of parameters. Should i be making a delegate with an address-of the bounds and wrapper instead? Thats what method.GetParameters() returns. Im confused because it doesnt appear to be complaining about that, rather just the count of parameters...
No description
Tijmen
TijmenOP13mo ago
waitwaitwait Oh no i think im calling an static method with an object target. Duhh And the first arg would become the object reference, hence the exception That was it!
Accord
Accord13mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server