Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection method GetMethod does not return the static method of a class on an iphone, but does on simulator

I have encountered a problem where the reflection method GetMethod is not returning a static method for a class. This is only going wrong on an actual iphone; on the simulator it is working correctly. I have tried the following:

MethodInfo methInfo = _type.GetMethod (methodName);

and

MethodInfo methInfo = _type.GetMethod (methodName, System.Reflection.BindingFlags.Static);

but neither of these return the method specified in methodName. The specified method does exist as shown by the fact that it works on the simulator. I have confirmed with the debugger that my member variable _type does contain the correct class type reference. The methods that I am trying to retrieve are declared as public in the class.

Has anyone encountered this before or know why this would work on the simulator but not on the actual iphone?

like image 637
BruceHill Avatar asked May 12 '11 01:05

BruceHill


1 Answers

Reflection, although not fully operational on iOS, works. The problem is that the linker is activated on the configuration for the device and is cutting off the method since it is not being used. The linker cannot "see" reflection calls.

If it is a custom object, decorate it with the PreserveAttribute:

[Preserve(AllMembers=true)]
public class MyClass
{}

If it is an object from the SDK, you have two choices:

  1. Disable the linker completely. This is not good since the final size will be big.
  2. Use the method once in your code directly so that the linker will know it is there and will not strip it.
like image 61
Dimitris Tavlikos Avatar answered Nov 10 '22 18:11

Dimitris Tavlikos