samedi 26 novembre 2016

C#/Unity Reflection: TargetException: Object does not match target type

When I try to run the following code, there is an error on the mi.Invoke line, saying "TargetException: Object does not match target type."

I've seen other answers on SO and they all say it's a problem with the first argument of Invoke being "this" rather then the type itself, which is not the case here.

The MethodInfo creation line and invoke line both use the same variable "type2", but it says they are different. How do I fix this error?

    //In this example VoxelType is "ConveyorBelt". This is a class with a public Update method, in the same namespace.
    Type type2 = getTypeByName (VoxelType)[0]; //Get voxel type
    print (type2.Namespace); //Returns null
    print (VoxelType); //Returns ConveyorBelt
    print (type2);//Returns ConveyorBelt
    MethodInfo mi = type2.GetMethod ("Update");
    mi.Invoke (type2, null); //Crashes everytime with TargetException





Aucun commentaire:

Enregistrer un commentaire