检查一个类型变量是否继承自另一个类型变量

问题描述 投票:0回答:1

TLDR:如果我有两个类型变量,如何检查一个变量是否继承于另一个变量?我想要的是这样的:

Type typeString = String;
Type typeInt = int;
Type typeNum = num;

inheritsFrom(typeString, typeInt) // false
inheritsFrom(typeInt, typeNum) // true

我无法使用镜像包,因为 Flutter 不支持它。

在我的 flutter 应用程序中,我有一个视图,其中显示数据网格。在这个视图中,我有整个列配置,其中每个列都有一个与之关联的配置对象,这告诉我一些事情,但在这种情况下,最重要的是它有一个通用类型,它告诉我的类型此列中的值。

以下是列配置类的一些代码:

class ColumnConfiguration<T> {
  final String columnName;
  final bool isEditable;
  final bool isDisplayed;
  final bool isSearchable;
  final int columnIndex;
  final List<TextInputFormatter> inputFormatters;
  Type get type => T;


  ColumnConfiguration({
    required this.columnName,
    required this.isEditable,
    required this.isDisplayed,
    required this.isSearchable,
    required this.columnIndex,
    required this.inputFormatters,
  });

  ColumnValue<T> createColumnValue(T? value) {
    return ColumnValue<T>(value, this);
  }
}

我在此视图中还有一个功能,可以将网格导出到 Excel。我做了一些检查来查看应导出哪些列,其中之一是我的 Excel 导出器类是否支持该类型。目前,我的 Excel 导出器有一个映射,该映射以类型作为键,以及一个将该类型的值映射到 Excel 单元格值的函数。这是地图:

static Map<Type, CellValue Function(dynamic)> cellValueConverterMap = {
    String: (value) => TextCellValue(value ?? ''),
    int: (value) => IntCellValue(value ?? 0),
    double: (value) => DoubleCellValue(value ?? 0),
    bool: (value) => TextCellValue((value ?? false) ? 'Ja' : 'Nej'),
    DateTime: (value) => TextCellValue(DateFormat('dd-MM-yyyy HH:mm:ss').format(value ?? DateTime.now())),
    // I would prefer to only have displayable object here, as EditDeliverySiteDto and Project both use the same
    // mapping and inherit from DisplayableObject. I have, however, not found a way to implement this. 
    // I simply can't figure out, how to check if one Type variable inherits from another Type variable.  
    DisplayableObject: (value) => TextCellValue(value?.displayText ?? ''),
    EditDeliverySiteDto: (value) => TextCellValue(value?.displayText ?? ''),
    Project: (value) => TextCellValue(value?.displayText ?? ''),
    Null: (value) => const TextCellValue(''),
  };

然后,当我调用 ExportToExcel 方法时,我会检查该类型是否受支持,如下所示:

ExcelExporter.exportToExcel(
      fileName: 'some_file_name.xlsx',
      sheetName: 'some_sheet_name',
      data: _someData,
      headers: listOfMyColumnConfigurations
          .where((cc) => cc.isDisplayed && ExcelExporter.cellValueConverterMap.containsKey(cc.type))
          .map((cc) => cc.columnName),
      startedCallBack: _showSnackBarGenerateExcelFileStarted,
      failedCallBack: _showSnackBarGenerateExcelFileError,
      completedCallBack: _showSnackBarGenerateExcelFileComplete,
    );

这个问题是,我无法检查 cc.type 是否是我的映射中任何键的继承类型,我只能检查它们是否完全相同。因此,当我有一个继承自 DisplayableObject 的项目的列配置时,我不想将项目添加到单元格转换器的映射中,我只想使用 DisplayableObject 转换器。

有办法做到这一点吗?

flutter dart
1个回答
0
投票

RuntimeType
仅用于调试目的,应用程序代码不应依赖于它。它可以被类覆盖以返回假值,并且在转换为 JS 时可能返回不可用的值。

您应该使用类,并将类与父类进行比较将按照您期望的方式使用运算符

is

class typeString{}
class typeInt extends typeNum{}
class typeNum{}

(typeString is typeInt) // false
(typeInt is typeNum) // true
© www.soinside.com 2019 - 2024. All rights reserved.