如何在 dart 中获取当前和调用函数的名称? [英] How to get the name of the current and calling function in dart?

查看:239
本文介绍了如何在 dart 中获取当前和调用函数的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# 有:

System.Reflection.MethodBase.GetCurrentMethod().名称

System.Reflection.MethodBase.GetCurrentMethod().Name

Dart 是否有类似的东西,但返回当前正在运行的函数以及调用当前运行函数的函数的名称的结果.

Does Dart have something similar but returns results for both the function that is currently being run as well as the name of the function that called the currently run function.

推荐答案

import 'dart:mirrors';
...
MethodMirror methodMirror = reflect(functionOne).function;

另见 https://github.com/dart-lang/sdk/issues/11916#issuecomment-108381556

这仅适用于 Dart 命令行 VM,但不适用于浏览器或 Flutter,因为不支持反射.

This will only work in the Dart command line VM, but not in the browser or Flutter because there reflection is not supported.

https://pub.dartlang.org/packages/reflectable 这样的代码生成解决方案可能在反射不可用的地方工作.

Code generation solutions like https://pub.dartlang.org/packages/reflectable might work instead where reflection is not available.

https://github.com/dart-lang/sdk/issues/28372 似乎相关.

这篇关于如何在 dart 中获取当前和调用函数的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆