I'm trying to create a delegate with Delegate.CreateDelegate I am getting the error:
ArgumentException: method arguments are incompatible System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <6073cf49ed704e958b8a66d540dea948>:0) System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure) (at <6073cf49ed704e958b8a66d540dea948>:0) System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) (at <6073cf49ed704e958b8a66d540dea948>:0)
This is my code:
for (int i = 0; i < statusMoves.Length; i )
{
StatusMovesMethods moveMethods = new StatusMovesMethods();
MethodInfo theMethod = moveMethods.GetType().GetMethod(statusMoves[i].name);
moveMethod = (Move.MoveMethod)Delegate.CreateDelegate(typeof(Move.MoveMethod), theMethod);
statusMoves[i].moveMethod = moveMethod;
}
And this is where moveMethod is initialized
public delegate void MoveMethod(Battler target);
public MoveMethod moveMethod;
CodePudding user response:
So I still don't know why it was giving me an error but I found a way to fix it, I found This post where someone had a similar question and I used the code from there like this:
void Start()
{
for(int i = 0; i < statusMoves.Length; i )
{
statusMoves[i].moveMethod = GetByName(moveMethods, statusMoves[i].name);
}
}
static Action GetByName(object target, string methodName)
{
MethodInfo method = target.GetType()
.GetMethod(methodName,
BindingFlags.Public
| BindingFlags.Instance
| BindingFlags.FlattenHierarchy);
// Insert appropriate check for method == null here
return (Action)Delegate.CreateDelegate
(typeof(Action), target, method);
}
Hope this helps someone