如何在 dart 中获取当前和调用函数的名称?

如何在 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 似乎相关.