The reflection method GetMethod does not return a static class method on the iphone, but does on the simulator

I ran into a problem when the GetMethod reflection method does not return a static method for the class. This only happens on a real iphone; On the simulator, it works correctly. I tried the following:

MethodInfo methInfo = _type.GetMethod (methodName); 

and

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

but none of them returns the method specified in the method name. The specified method exists, as shown by the fact that it works on the simulator. I confirmed with a debugger that my _type member variable contains the correct class type reference. The methods I'm trying to get are declared public in the class.

Has anyone come across this before or knew why this would work on a simulator but not on an actual iphone?

+6
source share
1 answer

Reflection, although not fully working on iOS, works. The problem is that the linker is activated in the configuration for the device and disables the method because it is not used. Linker cannot "see" reflection calls.

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

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

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

  • Disconnect the linker completely. This is not good, since the final size will be large.
  • Use the method once in your code directly, so that the linker knows that it exists and will not shed it.
+7
source

Source: https://habr.com/ru/post/887882/


All Articles