[英]How to reflect Dart library name?
有沒有辦法在 Dart 中反映特定的庫屬性(如庫名)? 您如何獲得庫 object 參考?
首先,並非所有 Dart 庫都有名稱。 事實上,大多數都沒有。 他們曾經使用過,但該名稱不再用於任何用途,因此大多數圖書館作者都不會費心添加名稱。
要對包括庫在內的任何內容進行反射,您需要使用dart:mirrors
庫,該庫在大多數平台上都不存在,包括 web 和 Flutter。 如果您沒有運行獨立 VM,則可能沒有dart:mirrors
。
使用dart:mirrors
,您可以通過多種方式獲取程序的庫。
library my.library.name;
import "dart:mirrors";
final List<LibraryMirror> allLibraries =
[...currentMirrorSystem().libraries.values];
void main() {
// Recognize the library's mirror in *some* way.
var someLibrary = allLibraries.firstWhere((LibraryMirror library) =>
library.simpleName.toString().contains("name"));
// Find the library mirror by its name.
// Not great if you don't know the name and want to find it.
var currentLibrary = currentMirrorSystem().findLibrary(#my.library.name);
print(currentLibrary.simpleName);
// Find a declaration in the current library, and start from there.
var alsoCurrentLibrary = reflect(main).type.owner as LibraryMirror;
print(currentLibrary.simpleName);
}
你想做什么,這需要進行反思?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.