根据这个测试,我不能用参数“list”调用方法“method”,因为参数类型与方法参数的类型不兼容。
我的测试哪里错了?
import "dart:mirrors";
void main() {
var list = new List<String>();
var listMirror = reflectClass(list.runtimeType);
// Is "List<String>" subtype of "List<String>"?
print(listMirror.isSubtypeOf(listMirror));
// Method with parameter "List<String>"
var method = (List<String> list) {};
var closure = reflect(method) as ClosureMirror;
var function = closure.function;
var parameter = function.parameters.first;
// Is "List<String>" subtype of "List<String>"?
print(parameter.type.isSubtypeOf(listMirror));
print(listMirror.isSubtypeOf(parameter.type));
// Invoke method with arg: "List<String>" on param "List<String>"
method(list);
}
输出:
true
false
false
附言
也许我不明白一些东西,但它仍然不起作用。
import "dart:mirrors";
void main() {
var stringMirror = reflectClass(String);
// Query "List<int> get codeUnits"
MethodMirror method = stringMirror.declarations.values
.where((e) => e.simpleName == #codeUnits).first;
// List<int>
var returnType = method.returnType;
print(returnType);
// List
var baseType = reflectClass(List);
print(baseType);
// List<int> is List
print(returnType.isSubtypeOf(baseType));
}
输出:
ClassMirror on 'List'
ClassMirror on 'List'
false