Search Unity

Question Prevent Default constructor getting stripped from types derived from abstract class.

Discussion in 'Scripting' started by Alverik, Jun 26, 2022.

  1. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Hi, recently I wanted to modularize some functionality to make it plug and play but I have an issue in the build. I basically have an abstract class which contains some abstract methods and properties. Then at runtime I collect all derived types and use Activator.CreateInstance() to instantiate the objects. It works perfectly in the Editor, but it breaks in the build because the compiler is stripping the default constructor from all the derived classes. I don't want to disable stripping but I want to avoid having to manually define default constructors and using [Preserve] for every single derived class I have or will create later (since I can't enforce a constructor I foresee times where me or someone using the code might forget to add a default constructor or use [Preserve] on it). So I tried [Preserve, RequireDerived] on the abstract class definition and [Preserve, RequiredMember] on a default constructor in the abstract class but neither worked. So I tried adding a link.xml file in one of my folders and put:


    <linker>
    <assembly fullname="Assembly-CSharp">
    <type fullname="MyNamespace.MyAbstractClass" preserve="all"/>
    </assembly>
    </linker>


    But it didn't work... I tried using .* and other variations, etc, but still nothing. Can someone tell me what I'm doing wrong?...
     
  2. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Or are abstract classes not supported for the Preserve/RequireDerived attributes and the link.xml?...