import 'dart:mirrors';
import 'dart_metadata.dart';
@Todo("me", "class")
class MirrorTest {
static num staticvar;
static num _staticvar;
num instancevar;
static const num constvar = 1;
final num fialvar;
@Todo("me", "var")
num metadatavar;
MirrorTest({this.fialvar, this.instancevar});
static void staticMethod() {
print("staticMethod");
}
void instanceMethod() {
print("instanceMethod");
}
@Todo("me", "method")
void metadataMethod() {}
}
void main(List<String> args) {
var classMirror = reflectClass(MirrorTest);
/// Named Contructor
var instanceMirror = classMirror.newInstance(
Symbol.empty, [], {Symbol("fialvar"): 0, Symbol("instancevar"): 2});
MirrorTest instance = instanceMirror.reflectee;
print(instance.runtimeType);
/// Class level metadata
classMirror.metadata.forEach((metadata) {
print(metadata.reflectee.who + "==>" + metadata.reflectee.what);
});
///invoke static method
classMirror.invoke(Symbol("staticMethod"), []);
///invoke instanceMirror method
instanceMirror.invoke(Symbol("instanceMethod"), []);
///print all fields name, metadatas
classMirror.declarations.forEach((Symbol key, DeclarationMirror value) {
if (value is VariableMirror) {
print("|" + value.simpleName.toString());
print("--" + value.runtimeType.toString());
print("--" + value.type.toString());
print("--" + value.type.reflectedType.toString());
value.metadata.forEach((metadata) {
print("---" + metadata.reflectee.who + "==>" + metadata.reflectee.what);
});
}
});
///print all mehtods,metadatas
classMirror.declarations.forEach((Symbol key, DeclarationMirror value) {
if (value is MethodMirror) {
print(value.simpleName);
value.metadata.forEach((metadata) {
print("---" + metadata.reflectee.who + "==>" + metadata.reflectee.what);
});
value.parameters.forEach((param) {
param.metadata.forEach((metadata) {
print(
"---" + metadata.reflectee.who + "==>" + metadata.reflectee.what);
});
});
}
});
}
Dart语言基础之反射
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...