英文:
C# .net core native AOT with reflection Activator.CreateInstance
问题
我正在尝试使用反射的 Activator.CreateInstance 方法生成带有以下参数的所需模块。
public TModule CreateModule<TModule>(params object[]? parameters) where TModule : ApplicationModule
{
/*
* 创建模块
*/
TModule? module = Activator.CreateInstance(typeof(TModule), parameters) as TModule;
if (module is null)
throw new Exception($"无法使用类型创建模块: {typeof(TModule).Name}");
/*
* 将其注册到待处理的模块
*/
_pendingModules.Add(module);
return module;
}
但它总是返回以下错误信息:
Unhandled Exception: System.MissingMethodException: 为类型 'Runtime.Reflection.ReflectionModule' 未定义无参数构造函数。
在 System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x121
在 Runtime.Application.Application.CreateModule[TModule]() + 0x35
在 EditorPlayer.Program.Main(String[]) + 0x107
在 EditorPlayer!<BaseAddress>+0x862f1b
尽管目标类具有默认构造函数,但仍然抛出相同的错误。我曾认为反射在本地 AOT 构建中是可用的。
英文:
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.
答案1
得分: 0
似乎编译器有时可能无法检测到代码库中使用的反射类型。在这种情况下,应创建并导入一个名为rd.xml的文件到项目设置中(rd代表运行时指令)。在这个xml文件中,您可以指定特定程序集中要使用的类型,或者直接指定类型,以便包含在本地AOT构建中。
对于泛型类型,似乎在泛型方法的末尾添加new()有助于编译器标记泛型类型,并将其导出到本地AOT构建中。
以下是一些可能有助于本地AOT反射的链接:
https://github.com/dotnet/corert/blob/master/Documentation/using-corert/reflection-in-aot-mode.md
https://github.com/dotnet/corert/blob/master/Documentation/using-corert/rd-xml-format.md
英文:
It seems compiler sometimes may fail to detect the reflection types used in a codebase. In such cases as these one should create and import an rd.xml file into project settings(rd stands for runtime directives). Here in this xml file you can specify the types in a specific assembly to be consumed or you can specify types directly so that can be included in the native aot build.
For generics types it seems that adding new() end of the generic method helps compiler to mark the generic type and to be exported in to the native aot build.
Here's some links that may help with the native aot reflection.
https://github.com/dotnet/corert/blob/master/Documentation/using-corert/reflection-in-aot-mode.md
https://github.com/dotnet/corert/blob/master/Documentation/using-corert/rd-xml-format.md
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论