Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reflection not working properly with the IL2CPP backend

Discussion in 'Scripting' started by alexisrabadan, Oct 20, 2016.

  1. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    81
    Just ran some tests and noticed that getting the properties for a transform at runtime on an iOS device only results in a few of its properties being returned, whereas the editor and android returns them all. It does however work fine when working with the Mono backend.

    Does anybody know why this is not working? Or is reflection simply not supported fully by IL2CPP?

    Code (CSharp):
    1.         BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
    2.         PropertyInfo[] properties = transform.GetType ().GetProperties (flags);
    3.         FieldInfo[] fields = transform.GetType ().GetFields (flags);
    4.  
    5.         print ("Properties");
    6.  
    7.         for (int i = 0; i < properties.Length; i++)
    8.             print (properties [i].Name);
    9.  
    10.         print ("Fields");
    11.  
    12.         for (int i = 0; i < fields.Length; i++)
    13.             print (fields[i].Name);
    14.  
    15.         print ("Test");
    16.  
    17.         PropertyInfo propertyInfo = transform.GetType ().GetProperty ("position", flags);
    18.         propertyInfo.SetValue (transform, new Vector3 (2, 2, 2), null);
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I think it's because stripping is done differently for iOS. Check this out here. This may or may not have anything to do with what you're seeing, as I don't build for iOS at all.
     
    Kiwasi likes this.
  3. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    81
    Yep, you're right. Adding a preserve all for UnityEngine solved the issue, but I'm not sure that is the best solution since I might not always need every unity class.

    Thanks
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its pretty much impossible to have the ability to fully reflect on code and to have code stripping.
     
    Bunny83 likes this.