當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Dart MirrorsUsed.metaTargets用法及代碼示例

dart:mirrors 庫中MirrorsUsed.metaTargets 屬性的用法介紹如下。

用法:

dynamic
          metaTargets
final

用作元數據時指示反射目標的類列表。另見targets

以下文本是非規範性的:

指定類列表的格式與指定 targets 的格式相同。但是,由於庫不能用作 Dart 中的元數據注釋,因此將庫添加到 metaTargets 的列表中沒有任何效果。特別是,將庫添加到metaTargets 不會使庫的類成為有效的元數據注釋以啟用反射。

如果 metaTargets 中指定的類的實例用作庫、類、字段或方法的元數據注釋,則該庫、類、字段或方法將添加到反射目標集。

示例用法:

library example;
@MirrorsUsed(metaTargets: "example.Reflectable")
import "dart:mirrors";

class Reflectable {
  const Reflectable();
}

class Foo {
  @Reflectable()
  reflectableMethod() { ... }

  nonReflectableMethod() { ... }
}

在上麵的例子中。 reflectableMethod 使用Reflectable 類標記為可反射,而該類又在metaTargets 注釋中指定。

nonReflectableMethod 方法缺少元數據注釋,因此在運行時無法反映。

相關用法


注:本文由純淨天空篩選整理自dart.dev大神的英文原創作品 metaTargets property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。