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

How do I write link.xml for generic class?

Discussion in 'Scripting' started by stopiccot_tds, May 15, 2019.

  1. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
    I'm getting an error during runtime:

    Code (csharp):
    1.  ExecutionEngineException: Attempting to call method 'System.Collections.Generic.List`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]::.cctor' for which no ahead of time (AOT) code was generated.
    I've added link.xml to my project:

    Code (csharp):
    1.  
    2. <linker>
    3.   <assembly fullname="mscorlib">
    4.     <type fullname="System.Collections.Generic.List" preserve="all"/>
    5.   </assembly>
    6.   <assembly fullname="netstandard">
    7.     <type fullname="System.Collections.Generic.List" preserve="all"/>
    8.   </assembly>
    9. </linker>
    10.  
    But it does not help. How can I fix this issue?
     
    DapremeeA likes this.
  2. DapremeeA

    DapremeeA

    Joined:
    Jul 2, 2019
    Posts:
    1
    I met the same error on my Unity Project during deserialization, did you find any solution ?
     
  3. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
    I've just created dummy class

    Code (CSharp):
    1. public class Preserver
    2. {
    3.     [UnityEngine.Scripting.Preserve]
    4.     public List<bool> doNoStripListBool = new List<bool>();
    5. }
     
    DapremeeA likes this.
  4. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
    Not the most elegant solution but it worked
     
    DapremeeA likes this.
  5. JoelAtMoodkie

    JoelAtMoodkie

    Joined:
    Oct 18, 2009
    Posts:
    896
    Believe I just found the solution for this elsewhere so thought I'd post it here:
    Code (csharp):
    1. <type fullname="System.Collections.Generic.List`1" preserve="all"/>
    `1 denotes a generic method with one generic parameter. For a Dictionary you would do `2.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    I don't think that this would help much. The issue is that AOT platforms do not support the dynamic generation of concrete types at runtime. So the specific combination of a List that has an element type of "bool" would create a separate concrete class. The AOT compiler analyse the code and generates native code for all types it "encounters". However if there is code that uses reflection or other dynamic means to construct a certain variant of a generic type that the AOT compiler could not see at compile time, the code for this variant would be missing. The AOT compiler can not generate code for ALL possible combinations as there are infinite.

    That's true. Generic types have the number of generic arguments added like this to the generic class name. Though the concrete types need to have the generic arguments bound to concrete types. For generic methods that means the AOT compiler needs to implement the method x times for the x possible variants you're using. As I said, usually the AOT compiler will pick up what variants you need based on your concrete usecases. Though it will cause issues when you or a system uses reflection to reach out to a certain member / method of a variant that may not appear anywhere else in your code.
     
  7. JoelAtMoodkie

    JoelAtMoodkie

    Joined:
    Oct 18, 2009
    Posts:
    896
    Makes sense. I was getting a very similar error message to the OP, but relating to stripping, but I now see this is a slightly different issue.

    (Here's the page where I found the information in case anyone is interested, but likely less relevant now everything is AOT: https://www.robinryf.com/blog/2017/08/22/unity-link-xml-file-definitions.html )
     
  8. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Code (CSharp):
    1. <linker>
    2.   <assembly fullname="mscorlib">
    3.     <type fullname="System.Collections.Generic.List" preserve="all"/>
    4.   </assembly>
    5.   <assembly fullname="netstandard">
    6.     <type fullname="System.Collections.Generic.List`1[[System.Boolean, mscorlib]]" preserve="all"/>
    7. </assembly>
    8. </linker>
     
    JoelAtMoodkie likes this.