Reflection 如果我知道一个函数的名称,是否可以得到它的参数?
Dart代码:Reflection 如果我知道一个函数的名称,是否可以得到它的参数?,reflection,dart,dart-mirrors,Reflection,Dart,Dart Mirrors,Dart代码: void hello(字符串名称){ 印刷品(姓名); } main(){ var funcName=“你好”; //如何获取参数“String name”? } 使用函数名作为字符串,“hello”,是否可以获取实际函数的参数字符串名 导入“dart:mirrors”; void hello(字符串名称){ 印刷品(姓名); } main(){ var funcName=“你好”; //获取当前库中的顶级函数 映射函数= currentMirrorSystem().isola
void hello(字符串名称){
印刷品(姓名);
}
main(){
var funcName=“你好”;
//如何获取参数“String name”?
}
使用函数名作为字符串,“hello”
,是否可以获取实际函数的参数字符串名
导入“dart:mirrors”;
void hello(字符串名称){
印刷品(姓名);
}
main(){
var funcName=“你好”;
//获取当前库中的顶级函数
映射函数=
currentMirrorSystem().isolate.rootLibrary.functions;
MethodMirror func=函数[常量符号(funcName)];
//一旦找到函数:获取参数
列表参数=函数参数;
用于(参数中的参数镜像参数){
字符串类型=MirrorSystem.getName(param.type.simpleName);
String name=MirrorSystem.getName(param.simpleName);
//....
打印(“$type$name”);
}
}
您可以使用
导入“dart:mirrors”;
void hello(字符串名称){
印刷品(姓名);
}
main(){
var funcName=“你好”;
//获取当前库中的顶级函数
映射函数=
currentMirrorSystem().isolate.rootLibrary.functions;
MethodMirror func=函数[常量符号(funcName)];
//一旦找到函数:获取参数
列表参数=函数参数;
用于(参数中的参数镜像参数){
字符串类型=MirrorSystem.getName(param.type.simpleName);
String name=MirrorSystem.getName(param.simpleName);
//....
打印(“$type$name”);
}
}
您通过反射(尚未完全完成)获得此信息:
库hello\u库;
导入“dart:镜像”;
void main(){
var mirrors=currentMirrorSystem();
const libraryName='hello_library';
var libraries=mirrors.findLibrary(常量符号(libraryName));
var length=libraries.length;
如果(长度==0){
打印(“未找到库”);
}否则,如果(长度>1){
打印(“找到多个库”);
}否则{
var method=getStaticMethodInfo(libraries.first,const Symbol('hello');
var参数=getMethodParameters(方法);
if(参数!=null){
for(参数中的参数镜像参数){
打印('name:${parameter.simpleName}:,type:${parameter.type.simpleName}');
}
}
}
}
MethodMirror getStaticMethodInfo(LibraryMirror库,符号methodName){
如果(库==null){
返回null;
}
返回library.functions[methodName];
}
列出getMethodParameters(MethodMirror方法){
if(方法==null){
返回null;
}
返回方法.参数;
}
void hello(字符串名称){
印刷品(姓名);
}
您通过反射(尚未完全完成)获得此信息:
库hello\u库;
导入“dart:镜像”;
void main(){
var mirrors=currentMirrorSystem();
const libraryName='hello_library';
var libraries=mirrors.findLibrary(常量符号(libraryName));
var length=libraries.length;
如果(长度==0){
打印(“未找到库”);
}否则,如果(长度>1){
打印(“找到多个库”);
}否则{
var method=getStaticMethodInfo(libraries.first,const Symbol('hello');
var参数=getMethodParameters(方法);
if(参数!=null){
for(参数中的参数镜像参数){
打印('name:${parameter.simpleName}:,type:${parameter.type.simpleName}');
}
}
}
}
MethodMirror getStaticMethodInfo(LibraryMirror库,符号methodName){
如果(库==null){
返回null;
}
返回library.functions[methodName];
}
列出getMethodParameters(MethodMirror方法){
if(方法==null){
返回null;
}
返回方法.参数;
}
void hello(字符串名称){
印刷品(姓名);
}
当您传递某个内容时,这将调用参数
。例如,func(参数1)
。当您在函数体
中获得此参数
时,调用的已不是参数
,而是参数
在何处
、如何
和什么
(参数
或参数
)您想要获取?我想要参数
:)当您传递某个东西时,这是调用参数
。例如,func(参数1)
。当您在函数体
中获得此参数
时,调用的已不是参数
,而是参数
<代码>在哪里,如何
和什么
(参数
或参数
)要获取?我要参数
:)我得到了字符串
?我得到了字符串
?