我有一个helper方法,它返回一个类型的公共静态只读字段列表:
public static ReadOnlyCollection<T> GetFields<T>()
where T : class
{
return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(x => x.IsInitOnly && x.FieldType == typeof(T))
.Select(x => (T)x.GetValue(null))
.ToList().AsReadOnly();
}
当我将项目转换为时。NET 8 AOT,我得到以下错误:
错误IL2090:“this”参数不满足
'动态访问的成员类型。PublicFields在对的调用中
’系统。类型GetFields(BindingFlags)“。的泛型参数“T”
“path\to\helper\GetFields()”没有匹配的注释。
源值必须声明至少与那些相同的要求
在分配给它的目标位置上声明。
如何修复此错误?单元测试调用helper方法只是为了确保特定类中的常量字段是唯一的,并遵循某些语法约定。