lundi 19 juin 2023

C# .net core native AOT with reflection Activator.CreateInstance

I am trying to use reflection's Activator.CreateInstance method to generate the required module with parameters like below.

 public TModule CreateModule<TModule>(params object[]? parameters) where TModule : ApplicationModule
    {
        /*
         * Create module
         */
        TModule? module = Activator.CreateInstance(typeof(TModule), parameters) as TModule;
        if (module is null)
            throw new Exception($"Failed to create module with type: {typeof(TModule).Name}");

        /*
         * Register it to the pending modules
         */
        _pendingModules.Add(module);

        return module;
    }

But it always return

  Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'Runtime.Reflection.ReflectionModule'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x121
   at Runtime.Application.Application.CreateModule[TModule]() + 0x35
   at EditorPlayer.Program.Main(String[]) + 0x107
   at EditorPlayer!<BaseAddress>+0x862f1b

Even though the target class has default constructor it still throws the same error. I thought reflection was present in native AOT builds.





Aucun commentaire:

Enregistrer un commentaire